We will create a Live Activity with a Stopwatch implementation for a Flutter project. The project will include a Flutter-side implementation as well as native implementation in the Xcode project.
Flutter side implementation
We will create a class that will implement three methods:
startLiveActivity()
updateLiveActivity()
stopLiveActivity().
We will be building a stopwatch app, and to pass the stopwatch timer data to native methods, we need to create a data model and send it to the method channel invocation in a JSON-like map format.
Each method will include the necessary code to call the corresponding native methods.
This was the initial setup required to run it on the Flutter side. Following this, a basic stopwatch app UI with the necessary method calls was implemented.
Native side implementation
Xcode project configuration
Open your project on Xcode and in General Tab set the minimum deployment to iOS 16.1
In Info.plist add a new key NSSupportsLiveActivities and set its value to YES (Boolean).
Info.plist
From action bar, select File > New > Target and search for WidgetExtensionand create a new widget.
Code side implementation
Let’s create a class called LiveActivityManager that will manage the live activities for our Dynamic Island. This class will contain three methods: startLiveActivity(), updateLiveActivity(), and stopLiveActivity().
Swift
Each method has its specific functionality. startLiveActivity() (as the name suggests) is responsible for initiating the Live Activity, which triggers the Dynamic Island functionality. Similarly, stopLiveActivity() and updateLiveActivity() handle stopping the Live Activity and updating the data displayed on the Dynamic Island.
Next, open StopWatchDIWidgetLiveActivity.swift and modify the StopwatchDIWidgetAttributes struct (as shown in the snippet). This attribute struct serves as the data model that holds the information to be displayed on the UI (directly from Flutter) and can also modify the UI as needed.
Swift
Now, the only thing left is the UI for the Dynamic Island! (Woohoo, we’ve come this far!)
The entire UI for the Dynamic Island needs to be created using SwiftUI. For this article, a simple UI has been designed (but feel free to customize it as needed).
This UI code should be written inside the StopwatchWidgetLiveActivity struct. Remove the existing code from the struct, and you can follow the code below:
SwiftUI
Don’t forget to also modify the AppDelegate code.
AppDelegate.swift
And that’s it! That covers everything needed to implement the Dynamic Island and link it to the Flutter code!
Pushwoosh code implementation
The Pushwoosh Flutter plugin provides two methods for managing Live Activity in the project.
The defaultSetup() method allows you to manage the structure of Live Activity and tokens on the Pushwoosh side.
Call this method during the application initialization.
During the initialization of the app, Pushwoosh will send your push-to-start token to the server, which will later allow you to start the Live Activity via an API call.
To start the Live Activity, you need to make an API request. Below is an example of the request:
If you want to start a simple Live Activity, for example, from your app, you can use the following method in Flutter: defaultStart(String activityId, Map<String, dynamic> attributes, Map<String, dynamic> content)
This method can be called on any event that triggers the start of the Live Activity.