live_updates 0.0.3
live_updates: ^0.0.3 copied to clipboard
iOS Live Activities and Android Live Update notifications with one Dart API. Built on FFIgen and JNIgen, no platform channels.
live_updates #
iOS Live Activities and Android Live Update notifications with one Dart API.
- One API to start, update and end a live update: title, body, progress, icon and a tap route.
- iOS 17.2+: ActivityKit Live Activities on the Lock Screen and in the Dynamic Island, with a ready-made SwiftUI widget you generate and customize.
- Android: ongoing notifications that Android 16 renders with
ProgressStyleand, on builds that support promoted notifications (Android 16 QPR1 and later), promotes to Live Updates with a status bar chip. Older Android versions show a regular ongoing notification with a progress bar. - Remote updates: iOS push-to-start and per-activity push tokens as streams; Android updates from a received payload (FCM glue lives in
live_updates_fcm). - No platform channels: native code is called through FFIgen (iOS) and JNIgen (Android). No third-party dependencies beyond the official
objective_candjniinterop runtimes. - Other platforms are a safe no-op.
Status: 0.x. The API may still change between minor versions.
Platform support #
| iOS | Android 16+ | Android 7–15 | Other | |
|---|---|---|---|---|
| Start / update / end | Live Activity | Live Update (when promoted) | Ongoing notification | no-op |
| Progress | ✓ | ProgressStyle |
progress bar | |
| Metrics, semantic colors | – | Android 17+ | – | |
| Tap route | ✓ | ✓ | ✓ | |
| Lifecycle events | ✓ | ✓ | ✓ | |
| Push tokens | ✓ | – | – | |
| Remote payload | APNs (direct) | handleRemotePayload |
handleRemotePayload |
Requirements: Flutter 3.50+, iOS 17.2+, Android minSdk as set by Flutter.
Getting started #
flutter pub add live_updates
iOS #
-
Generate the widget extension from your app's root directory:
dart run live_updates:create_widgetThis writes
ios/LiveUpdatesWidget/, adds a LiveUpdatesWidget extension target toRunner.xcodeproj(using thexcodeprojRuby gem that ships with CocoaPods), setsNSSupportsLiveActivitiesand registers theliveupdates-<bundle id>URL scheme used for taps. Use--no-xcodeto only write the files and add the target yourself (File > New > Target > Widget Extension, deployment target 17.2).Flavors are supported: every build configuration (for example
Debug-dev,Release-prod) gets a widget bundle id and an App Group derived from that configuration's app bundle id (LIVE_UPDATES_APP_GROUPbuild setting). If the bundle id is defined in an.xcconfiginstead of the project, pass--app-group group.<bundle id>.The command needs Ruby with the
xcodeprojgem (included with CocoaPods). If it is missing, the command offers to rungem install --user-install xcodeprojfor you. -
Open the project in Xcode once and select a signing team for the new target.
-
For push updates, add the Push Notifications capability to the Runner target.
Manual setup
With --no-xcode only the widget sources are written. Then, in Xcode:
- File > New > Target > Widget Extension named
LiveUpdatesWidget(no Live Activity or configuration intent templates), deployment target 17.2, and replace its generated Swift files with the files fromios/LiveUpdatesWidget/. - Add
LiveUpdatesActionIntent.swiftto both the Runner and the widget target (Target Membership). - Runner
Info.plist: addNSSupportsLiveActivities= YES and a URL type with the schemeliveupdates-$(PRODUCT_BUNDLE_IDENTIFIER). - For images: add the App Groups capability with the same group to both
targets and set
LiveUpdatesAppGroupto that group in both Info.plist files.
The UI lives in ios/LiveUpdatesWidget/LiveUpdatesWidgetLiveActivity.swift;
edit it freely. Keep LiveUpdatesAttributes.swift identical to the plugin's
copy, because ActivityKit matches the two by name and shape.
Android #
Nothing to configure. The plugin declares POST_NOTIFICATIONS and
POST_PROMOTED_NOTIFICATIONS. Call LiveUpdates.requestPermission() before
starting on Android 13+.
Android only promotes a notification to a Live Update when the build supports
it, the user allows it (LiveUpdates.canPostPromotedNotifications()), and the
notification meets the system's criteria. Otherwise it stays a regular
ongoing notification.
Platform guidelines #
Both platforms limit what these surfaces are for. Follow Apple's Human Interface Guidelines for Live Activities and Android's Live Update guidance:
- Use them for ongoing, user-initiated, time-sensitive activities, such as a ride, a food order, navigation, a call or a sports score.
- Do not use them for ads, promotions, chat messages or reminders.
- Do not start them for something the user did not ask for. On Android set
AndroidLiveUpdateOptions(requestPromotion: false)for anything that is not a Live Update; it then stays a regular notification. - Do not bring back an update the user dismissed. On Android
updatefails for a dismissed update, and remoteupdatepayloads for it are ignored. - Keep
shortCriticalTextshort (Android suggests at most 7 characters) and metric labels to 10 characters or fewer.
Usage #
import 'package:live_updates/live_updates.dart';
if (await LiveUpdates.requestPermission()) {
final update = await LiveUpdates.start(
const LiveUpdateContent(
title: 'Order #1234',
body: 'Preparing',
progress: 0.1,
route: '/orders/1234',
android: AndroidLiveUpdateOptions(shortCriticalText: '10 min'),
),
);
await update.update(
const LiveUpdateContent(title: 'Order #1234', body: 'On the way', progress: 0.6),
);
await update.end(dismissAfter: const Duration(minutes: 5));
}
// Taps on a live update deliver its route. Only routes that one of the
// app's live updates carries are delivered, but treat them like any deep
// link: validate before acting on them.
LiveUpdates.onTap.listen((route) => router.go(route));
// Live updates still running, e.g. after an app restart.
final running = await LiveUpdates.active();
// Live updates that appear while the app runs, including remote starts.
LiveUpdates.onStarted.listen((update) => track(update));
// Lifecycle: stale (iOS), ended, dismissed (e.g. swiped away on Android).
update.states.listen((state) => print(state));
await LiveUpdates.endAll();
One live update per key #
When your data already has an id, use it as the key. Calling
startOrUpdate again updates the same live update instead of starting a
duplicate, and find gets it back after an app restart:
await LiveUpdates.startOrUpdate('order-1234', content);
final update = await LiveUpdates.find('order-1234');
On Android the key is the update's id and is the same as live_updates_id
in remote payloads, so the app and the server address the same notification.
On iOS the key is stored in the activity's attributes; a push-to-start can set
it with "attributes": {"key": "order-1234"}.
Buttons #
const LiveUpdateContent(
title: 'Order #1234',
actions: [
LiveUpdateAction(
id: 'cancel',
title: 'Cancel',
iosSystemImage: 'xmark',
requiresUnlock: true,
),
LiveUpdateAction(id: 'track', title: 'Track', opensApp: true),
],
);
LiveUpdates.onAction.listen((event) {
// event.updateId, event.actionId
});
- Buttons without
opensApprun in the background: on iOS throughLiveUpdatesActionIntent(aLiveActivityIntentthatcreate_widgetcompiles into both the app and the widget), on Android through a broadcast receiver. The press is delivered toonActionwhile the app runs. If the app was not running, it is queued and delivered once Dart listens; useopensApp: truewhen the press must be handled right away. opensApp: trueopens the app and delivers the press.- Background presses work on the Lock Screen without unlocking the device.
Set
requiresUnlock: trueon buttons with consequences, such as cancelling an order: iOS asks for Face ID or the passcode first, and Android 12+ asks the user to unlock.
Handling presses while the app is not running
Register a background handler to act on a press immediately, even when the app is not running. The plugin starts a headless Flutter engine, runs the handler and shuts the engine down:
@pragma('vm:entry-point')
Future<void> onBackgroundAction(LiveUpdateActionEvent event) async {
if (event.actionId == 'cancel') {
await api.cancelOrder(event.updateId);
final updates = await LiveUpdates.active();
await updates
.where((u) => u.id == event.updateId)
.firstOrNull
?.end(dismissAfter: Duration.zero);
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await LiveUpdates.setBackgroundActionHandler(onBackgroundAction);
runApp(const App());
}
-
The handler runs only when no
onActionlistener is active; otherwise the press goes toonActionas usual. (On iOS the system may launch your app in the background for the press; if your app subscribes toonActionat startup, that listener receives it.) -
It must be a top-level or static function with
@pragma('vm:entry-point'), and it has limited time (about 9 seconds on Android, 25 seconds on iOS). -
live_updates itself works in the handler. On iOS, other plugins that use platform channels need to be registered for the background engine:
// AppDelegate.swift LiveUpdatesPlugin.setPluginRegistrantCallback { registry in GeneratedPluginRegistrant.register(with: registry) }On Android, plugins are registered automatically.
-
Apps that set up the widget with an older version should run
dart run live_updates:create_widget --forceto updateLiveUpdatesActionIntent.swift. -
Android shows up to three actions. On iOS keep labels short: the whole content must stay under ActivityKit's 4 KB limit.
Android 17 metrics and semantic colors #
On Android 17 a Live Update can show up to three values, such as a countdown
and a distance, with MetricStyle instead of the progress bar. Timers and
stopwatches are counted by the system, so they keep running between updates.
Semantic styles let Android color the body text, the progress bar or a metric
by meaning (info, safe, caution, danger); the system picks the colors and
applies them to promoted Live Updates.
LiveUpdateContent(
title: 'Order #1234',
body: 'Driver is nearby',
progress: 0.8,
android: AndroidLiveUpdateOptions(
metrics: [
AndroidMetric.timer('Arrives in', eta),
AndroidMetric.decimal('Distance', 0.6, unit: 'km'),
],
criticalMetric: 0, // may be shown in the status bar chip
bodySemanticStyle: LiveUpdateSemanticStyle.safe,
),
);
Below Android 17 metrics and semantic styles are ignored and the progress bar is shown as before. The same keys work in remote payloads (see docs/testing-remote.md).
Images #
Pass a picture with the content. It becomes the thumbnail in the iOS widget template (Lock Screen and Dynamic Island) and the large icon of the Android notification:
const LiveUpdateContent(
title: 'Courier on the way',
image: LiveUpdateImage.asset('assets/courier.png'),
// or LiveUpdateImage.file(path), .memory(bytes), .network(Uri.parse(url))
);
Images are scaled to maxDimension pixels (default 256), stored as PNG and
reused when identical; files unused for a day are cleaned up.
On iOS the widget extension cannot read the app's files, so images go through
an App Group. create_widget sets one up by default (group.<app bundle id>,
or --app-group <id>): it adds the entitlement to both targets and writes the
LiveUpdatesAppGroup Info.plist key the plugin and the widget read. With
automatic signing Xcode registers the group; otherwise add it in your Apple
Developer account. A customized widget loads an image with
LiveUpdatesImages.url(context.state.image).
Icons #
iconName names a drawable (or mipmap) on Android and an image in the widget
extension's asset catalog on iOS. Without it, the app icon (Android) or a
system symbol (iOS widget template) is used.
Remote updates #
iOS (APNs) #
// Start activities from your server (iOS 17.2+).
LiveUpdates.pushToStartTokens.listen((token) => api.registerPushToStart(token.hex));
// Update a running activity from your server.
final update = await LiveUpdates.start(
const LiveUpdateContent(title: 'Order #1234'),
pushUpdates: true,
);
update.pushTokens.listen((token) => api.registerActivity(update.id, token.hex));
Send pushes with apns-push-type: liveactivity and topic
<bundle id>.push-type.liveactivity. The content-state uses the same keys as
LiveUpdateContent: title, body, progress, iconName, route, extra.
For push-to-start, set "attributes-type": "LiveUpdatesAttributes" and
"attributes": {}.
iOS broadcast (iOS 18+) #
For updates that many users share, such as a match score, subscribe the activity to an APNs broadcast channel instead of collecting per-activity tokens. One push to the channel updates every subscribed device.
final update = await LiveUpdates.start(
const LiveUpdateContent(title: 'FC Tokyo 2 - 1 Kashima', body: '78 min'),
iosChannelId: channelIdFromYourServer,
);
- Enable Broadcast Capability for the App ID in your Apple Developer account (Identifiers > Push Notifications); Xcode cannot enable it.
- Your server creates channels with APNs' channel management API and sends
update/endpushes to them;tool/send_push.dart apns-channelandapns-broadcastshow both requests. - Broadcasts cannot start activities. Start locally with
iosChannelId, or send a push-to-start withinput-push-channelso the started activity joins the channel. iosChannelIdcannot be combined withpushUpdates, and throws aLiveUpdateExceptionbefore iOS 18. On Android, use FCM topic messages with the payload below.
Android #
Deliver a data message and pass it to LiveUpdates.handleRemotePayload:
| Key | Value |
|---|---|
live_updates_event |
start, update or end |
live_updates_id |
Your id for the update (used by later messages) |
live_updates_content |
Content as a JSON string, same keys as above |
live_updates_dismiss_after |
Optional, seconds to keep final content on end |
With Firebase, live_updates_fcm
does this for you.
See docs/testing-remote.md
for a step-by-step verification guide and tool/send_push.dart for a sender.
How it works #
- iOS: a small
@objcSwift class (LiveUpdatesBridge) wraps ActivityKit for one fixedActivityAttributestype,LiveUpdatesAttributes. Dart calls it through FFIgen-generated Objective-C bindings. - Android: a Java class (
LiveUpdatesBridge) builds the notifications. Dart calls it through JNIgen-generated bindings.
License #
MIT