Live Activities for the Flutter Plugin
Integrate Live Activities into your Flutter app to display real-time updates on the iOS Lock Screen and Dynamic Island. AXP
For information about Live Activities, including overview, use cases, and how to send Live Activities via the Push API, see iOS Live Activities.
App setup
Using the AirshipPluginExtender, make a call to LiveActivityManager.shared.setup to configure any Live Activities for the app. Call configurator.register for each Live Activity type that your application defines and include a block on how to parse the name of the activity that you will use to track on Airship. This name will be used to send updates through APNS.
import Foundation
import AirshipKit
import AirshipFrameworkProxy
import ActivityKit
// This class header is required to be automatically picked up by the Airship plugin:
@objc(AirshipPluginExtender)
public class AirshipPluginExtender: NSObject, AirshipPluginExtenderProtocol {
public static func onAirshipReady() {
if #available(iOS 16.1, *) {
// Will throw if called more than once
try? LiveActivityManager.shared.setup { configurator in
// Call for each Live Activity type
await configurator.register(forType: Activity<SportsActivityAttributes>.self) { attributes in
// Track this property as the Airship name for updates
attributes.gameID
}
}
}
// other setup
}
}Starting Live Activities
For any Live Activities configured, you can start a new one using the start method:
if (Platform.isIOS) {
LiveActivityStartRequest startRequest = LiveActivityStartRequest(
attributesType: 'SportsActivityAttributes',
attributes: {
"gameID": "sports-game-123",
},
content:
LiveActivityContent(status: 'Game Pending', relevanceScore: 0.0));
await Airship.liveActivityManager.start(startRequest);
}Updating Live Activities
To update, use update, but you will need the activity ID.
if (Platform.isIOS) {
List<LiveActivity> activities = await Airship.liveActivityManager.listAll();
LiveActivity? activity = activities
.where((activity) => activity.attributes.gameID == 'sports-game-123')
.firstOrNull;
if (activity != null) {
LiveActivityContent content = LiveActivityContent(
state: {'status': 'Game starting!'},
relevanceScore: 0.0,
);
LiveActivityUpdateRequest updateRequest = LiveActivityUpdateRequest(
attributesType: 'SportsGameAttributes',
activityId: activity.id,
content: content,
);
await Airship.liveActivityManager.update(updateRequest);
}
}Ending Live Activities
To end is similar to update. Use end with the activity ID:
if (Platform.isIOS) {
List<LiveActivity> activities = await Airship.liveActivityManager.listAll();
LiveActivity? activity = activities
.where((activity) => activity.attributes.gameID == 'sports-game-123')
.firstOrNull;
if (activity != null) {
LiveActivityStopRequest stopRequest = LiveActivityStopRequest(
attributesType: 'SportsGameAttributes',
activityId: activity.id,
dismissalPolicy: LiveActivityDismissalPolicyDefault(),
);
await Airship.liveActivityManager.end(stopRequest);
}
}Categories