liveactivity-rs
Live Activities for Rust applications, behind one interface: iOS ActivityKit and Android Live Updates by default, and any other backend in their place.
use ;
let live = builder.build?;
let order = live
.start
.await?;
live.update
.await?;
live.end.await?;
Platforms
| Platform | Engine | Shows | From |
|---|---|---|---|
| iOS, iPadOS | activitykit (ActivityKit, WidgetKit) | the Lock Screen, the Dynamic Island, StandBy, a paired Apple Watch, CarPlay | iOS 16.1, iPadOS 17 |
| Android | liveupdate (an AAR) | an ongoing notification, promoted to a Live Update; a Wear OS watch | Android 8 (API 26); Live Updates from Android 16 |
| A server | features = ["server"] |
pushes through APNs and FCM | any target |
| Anything else | your LiveActivityBackend |
what it draws | any target |
your application
│
LiveActivity: one contract, one JSON
┌──────────┴──────────┐
ActivityKitBackend LiveUpdateBackend
activitykit liveupdate
ActivityKit Android notifications
▲ ▲
APNs push FCM data message
└──────────┬──────────┘
push::Sender (feature "server")
Install
[]
= "0.16"
A server that sends pushes takes the same version with default-features = false
and features = ["server"]: the sender, without the application's side
(Pushes from a server).
The crate uses activitykit for
iOS and liveupdate for Android as
they are. It compiles on every target; where there is no platform, the default
is UnsupportedBackend: the queries answer that nothing is supported and every
other call answers UNSUPPORTED_PLATFORM.
| Document | For |
|---|---|
| docs/contract.md | every command, its JSON, the limits, the errors, the events and the layout tree |
| docs/ios-setup.md | the widget extension, the App Group and pushes of an iOS app |
| docs/android-setup.md | the AAR, the permissions and the files of an Android app |
| docs/framework-integration.md | writing a framework plugin: commands, events, lifecycle, threads |
| docs/server-push.md | a server's pushes: the key, the targets, the messages and what came back |
| docs/design/engine-compatibility | a watch's Ongoing Activity, an event of an unknown kind, constructors and enums that may grow, on liveupdate 0.7 |
| docs/design/engine-small-family | the small region of a layout, for a paired Apple Watch and CarPlay on iOS, on activitykit 0.6 |
| docs/design/engine-interaction | toggles, staleness, scheduled and transient starts and alert sounds, on activitykit 0.5 |
| docs/design/engine-extensions | what the engines opened in activitykit 0.4 and liveupdate 0.2, what this crate does with it, and what is still planned |
The two sides
Everything goes through one builder. An application says where events go; a framework plugin or a platform says which backend runs the commands. Whatever is not said is the default.
let live = builder
.backend // the supporting side; without it, the platform's default
.event_sink // the using side; without it, events are dropped
.build?;
| You are | You write |
|---|---|
| an application | .event_sink(..), then the calls of Features |
| a framework plugin | an EventSink that forwards events to the framework's own, commands that call LiveActivity, and a backend only when the framework has a native side of its own |
| a platform | a LiveActivityBackend |
Features
Every snippet below is a function of
examples/features.rs, which
compiles and runs:
| Feature | Call | iOS | Android |
|---|---|---|---|
| Connect | LiveActivity::builder() |
✓ | ✓ |
| Progress and timer | start, update, end, list |
✓ | ✓ |
| Layouts | start_layout, update_layout, end_layout, list_layout |
✓ | ✓ projected onto a notification |
| Buttons and taps | a button node, Event::Action |
✓ from iOS 17.2; the label alone before | ✓ up to three |
| Toggles | a toggle node, ActionEvent.on |
✓ a switch, from iOS 17.2 | ✓ an action, with a check mark while on |
| Staleness | when_stale() and unless_stale() on a node |
✓ drawn by the system when the time has passed | drawn as while fresh, reported as layout.ifStale; the sink is told stale |
| StandBy | when_fullscreen() and unless_fullscreen() on a node |
✓ iOS 17, while the Lock Screen fills the screen of a charging iPhone on its side | never full screen: a node of StandBy is left out, its counterpart shown |
| A tap that runs your own code | AndroidOptions::with_target, NotificationAction.target, Event::Handled |
ignored | ✓ a service or a receiver of the application, where the device offers action_targets |
| A watch | Layout.small; AndroidOptions::with_wear_ongoing_activity, AndroidPresentation.wear_ongoing_activity, AndroidOptions::with_local_only, AndroidOptions::with_watch_mirror |
✓ a paired Apple Watch, in its Smart Stack from iOS 18: small where the widget extension sets ActivityKitSmallFamily, else the compact parts, reported as layout.small. The Ongoing Activity is ignored |
✓ an application on the watch, Wear OS 3 to 6, with androidx.wear:wear-ongoing: the activity's icon on the watch face, its Ongoing Activity, which one activity can do without. A phone's ongoing notification is not bridged to a paired watch, and a Live Update only to some; with_local_only(true) keeps one off it where the bridge honors it (Galaxy Wearable does not), and with_watch_mirror(true) sends it there as a mirror. small is not drawn, reported as layout.small |
| A car | Layout.small |
✓ CarPlay, from iOS 26: small as on a watch, else the compact parts |
none: Android Auto shows the notifications of messaging applications built for it, and Android's documents name no place there for a Live Update. small is reported as layout.small |
| Links | a link node, Layout.url |
✓ | ✓ the notification and its actions open them, where the device opens URLs |
| Accessibility, motion, privacy | members of a node and of a style | ✓ | a label is the text of what a notification cannot draw; motion and privacy are named as left out |
| Alerts | LayoutUpdateInput.alert |
✓ | ✓ where the device alerts again; else silent, reported as alert |
| A scheduled start | LayoutStartInput.scheduled |
✓ from iOS 26 | ✓ with liveupdate's library 0.4; on the minute with the exact-alarm permission |
| A transient activity, an alert's sound | LayoutStartInput.ios, Alert::with_sound |
✓ | not kept; a sound is reported as alert.sound |
| Relevance | LayoutSnapshot.relevance |
✓ | ignored |
| Android content | LayoutSnapshot.android |
ignored | ✓ |
| A last content that stays | EndPolicy::After |
✓ up to four hours | ✓ where the device offers it |
| A lock screen version, a foreground service | LayoutStartInput.android |
ignored | ✓ where the device offers them |
| Images | write_image, an image node |
✓ needs an App Group | ✓ as the large icon |
| Pushes | LayoutStartInput.push, push_tokens |
✓ | none |
| Pushes from a server | push::Message, push::Target, push::Sender (feature server) |
✓ APNs | ✓ FCM, with the feature android-push and liveupdate's library 0.12: the device runs the push as the application's call, reported as Event::Pushed |
| Events | the event sink, sync_events |
✓ as they come | ✓ on sync_events |
| Capabilities and permissions | capabilities, permissions, request_permissions, open_promotion_settings |
✓; no settings of promotion | ✓ the dialog is the host's; the settings of Live Updates open from a tap |
| Blocking calls | wait |
✓ off the main thread | ✓ |
Connect
let live = builder
.event_sink
.build?;
Without .backend(..) the backend is the platform's own. Keep the
LiveActivity in application state: it is cheap to clone and safe to use from
any thread. liveactivity::connect_native(sink) is the same in one call.
Progress and timer activities
A title, a status, and a progress from 0 to 1 or a countdown. They need no layout and look native on both platforms.
let order = live
.start
.await?;
// An update sends the whole content again, under a higher revision.
live.update
.await?;
live.end.await?;
// A countdown between two times, in epoch milliseconds.
let tea = live
.start
.await?;
live.end.await?;
live.list() answers the activities the backend knows. Every request has a
constructor that takes what is required (StartInput::new(key, Snapshot::progress(..)),
UpdateInput::new, EndInput::after(id, at), ImageInput::new, AndroidContent::new),
and the enums may grow: build on the constructors and match with a _ arm, and
the code compiles against the next version. So do activitykit's layout types:
Layout::new(lock_screen) with .island(..), .background(..), .url(..) and
.small(..), Island::new(), Style::new(), Color::adaptive(light, dark).
Layout activities
A tree of nodes, fixed at start, and values that updates replace. iOS draws
the tree on the Lock Screen and in the Dynamic Island; Android projects it
onto a notification and names what it left out in the record's
degraded_features. The nodes and styles are in
docs/contract.md.
use ;
let layout = new;
let values = values! .into_inner;
let start = new;
let record = live.start_layout.await?;
// Every value again, under a higher revision.
let values = values! .into_inner;
live.update_layout
.await?;
live.end_layout.await?;
A request that comes as JSON is read with
parse_request("start_layout", json)?, which checks it against the contract.
On the Lock Screen the card has no margin and shows a narrower layout in its middle: give the root a padding, and a node that should reach an edge a frame of infinite width (placement).
Buttons and taps
A button(action_id, label) node is a button on iOS and a notification action
on Android. Its tap reaches the sink as Event::Action, with the action_id,
the activity's id and its key. The tap does not change the activity: send an
update if the button should change something.
Toggles and staleness
use ;
let layout = new;
let values = values! .into_inner;
let mut snapshot = new;
snapshot.stale_at = Some;
let record = live.start_layout.await?;
A tap on the toggle reaches the sink as Event::Action with on, the state
the person asked for. The application sends an update with that value, or does
not when it refuses the change:
match event
- iOS draws a switch (iOS 17.2; the label alone before). Once the stale date has passed, the system draws the stale nodes by itself, without an update and without the application running.
- Android shows the toggle as one of the notification's three actions,
titled by its label, with
✓before it while the value is on. It draws the content as it is while fresh and nameslayout.ifStale: nothing redraws a notification when a time passes. The sink is toldstale(Events), and an application that wants the notification to say so sends an update then.
StandBy
An iPhone that charges on its side shows the activity full screen, in StandBy (iOS 17), with room for more. A node can be drawn only there, or only outside it:
let layout = new;
A notification is never full screen: Android leaves the StandBy node out.
Links, accessibility and motion
Members of the layout, which iOS draws: a URL that a tap opens the application with, what VoiceOver reads of a node, how a node comes and goes, and what a node marks as private. An iPhone 16 with iOS 27.2 still showed a private node on the locked Lock Screen: keep what no one else may see out of an activity.
use ;
let layout = new
// A tap anywhere else on the activity.
.url;
let record = live.start_layout.await?;
// On Android the links open too, and the record names layout.transition.
println!;
The application receives a URL as any URL it is opened with: onOpenURL on
iOS, the intent of the activity that declares the URL's scheme on Android. A
style's content_transition says how its text changes with an update, and
IslandExpanded::priority, IslandExpanded::below_if_too_wide and
Island::margins are the Dynamic Island's own options.
On Android the same layout gives the notification what it can show of this:
Layout.urlis what a tap on the notification opens, and alinkis an action that opens its URL. When nothing in the application opens a URL the request does not fail: the links are left out and named indegraded_features.- A timer beside a progress is the clock in the notification's header.
- A node that a notification cannot draw, such as a symbol or a gauge, is shown as the text of its label and value. A button whose label is a symbol is titled by the symbol's label; without one it is no action.
- A node with
Accessibility::new().hidden()is decoration and is left out without being named.
Alerts and relevance
let mut snapshot = new;
// Which of the application's activities the Dynamic Island shows first.
snapshot.relevance = Some;
let mut update = new;
update.alert = Some;
let record = live.update_layout.await?;
On iOS the device lights up, the Dynamic Island expands and an Apple Watch
shows the title and the body. On Android the notification alerts again with
that update where the device offers it (android_presentation.alert);
elsewhere the update is silent and record.degraded_features names alert.
An update without an alert is silent everywhere.
Start options and sounds
// A sound of the application's bundle. Without one the system plays its own.
let alert = new.with_sound;
let mut later = start.clone;
later.scheduled = Some;
let record = live.start_layout.await?;
// Until its time the activity is pending: it holds its key, takes no update, and can be ended.
assert_eq!;
live.end_layout.await?;
// An activity that the system ends when the person locks the device or leaves the application (iOS 18).
let mut transient = start;
transient.ios = Some;
live.start_layout.await?;
- A scheduled start begins at
at, in epoch milliseconds, with the alert the system shows then, also while the application does not run: iOS 26, and Android whereandroid_presentation.scheduled_startsays so (liveupdate's library 0.4 or later). The sink receivesStatewithactivewhen it begins. Android begins it at its time with the permission for exact alarms (exact_start), and up to ten minutes late without, an hour while the device dozes; see Android setup. Elsewhere the start isUNSUPPORTED_FEATURE. iosholds what only iOS reads, asandroiddoes for Android. It is no part of what makes two starts the same activity.- A sound is the name of a file in the application's bundle or in its
Library/Sounds, for the alert of an update as well. Android alerts with the sound of its channel and namesalert.sound.
Android content
What the notification shows, in Android's own terms, instead of the projection of the layout: a progress with segments and points, metrics, the text of the status chip, icons of actions. The types are liveupdate's, re-exported. Each is built on its constructor, which takes what is required: a member that a later version adds is then left at its default, and the code still compiles.
let segment = ;
// Which notification styles the device draws.
let styles = live.capabilities.await?.android_presentation;
let segmented = styles.is_some_and;
let mut snapshot = new;
let segments = if segmented else ;
snapshot.android = Some;
live.update_layout.await?;
A content may also name a URL that the notification opens, an action that
opens one, a clock in the header, what a secure lock screen shows and the
application's own look. The device says which of them it offers, and a content
that uses one it does not offer is UNSUPPORTED_FEATURE:
let capabilities = live.capabilities.await?;
let offered = capabilities.android_presentation.unwrap_or_default;
snapshot.android = Some;
A snapshot without a content is projected from the layout, in the layout's
own style. A snapshot may have another style than the activity shows where the
device offers style_change; elsewhere that is UNSUPPORTED_FEATURE, and the
activity keeps what it shows. The backend needs the app's files for a content:
see Platform setup. Timer is liveactivity::liveupdate::Timer.
Android options
What only Android reads of a start. iOS ignores it.
let mut options = new;
// What a secure lock screen shows in place of the content of a private activity.
options.public_version = Some;
// The notification is the one a foreground service of the application holds.
if offered.foreground_placement
start.android = Some;
let record = live.start_layout.await?;
if record.degraded_features.iter.any
The public version goes with every snapshot that has none of its own. One
running activity has the foreground placement at a time, and a device that
does not offer it answers UNSUPPORTED_FEATURE. The service is the
application's: docs/android-setup.md.
In a watch application an activity is on the watch face too, as its Ongoing
Activity, where the device says android_presentation.wear_ongoing_activity
(docs/android-setup.md). One activity can
stay in the tray, in all of its snapshots:
start.android = Some;
A phone's activity can stay off a paired watch, where the device says
android_presentation.local_only:
start.android = Some;
Or reach one through a mirror, a second notification that a watch's bridge
takes, where the device says android_presentation.watch_mirror:
start.android = Some;
A button can run the application's own service or receiver at once, with no
screen, as a Live Activity runs an App Intent: name its target by the action
id. The component takes the tap with LiveUpdateActions.take, and the sink
gets Event::Handled
(docs/android-setup.md):
let record = new;
start.android = Some;
Ending
// The platform's default: iOS keeps the activity on the Lock Screen for a while, Android removes it.
live.end_layout.await?;
// Or keep the last content on screen until a time, with a final snapshot.
let mut end = new;
end.policy = After ;
end.snapshot = Some;
live.end_layout.await?;
LayoutEndInput::immediate(id) removes the activity at once. With
EndPolicy::After iOS keeps the ended activity for at most four hours. Android
keeps the last content, no longer ongoing, where the device offers
delayed_end, and answers UNSUPPORTED_FEATURE elsewhere, leaving the
activity as it was. Template activities end the same way, with EndInput.
Images
live.write_image.await?;
Write the PNG first, then show it with an image node whose source is
{ "file": "badge.png" }. On iOS it goes to the App Group that the widget
extension reads. On Android the first such image of a layout becomes the
notification's large icon.
Pushes
iOS only. A start asks for a token, the sink gets it as Event::PushToken,
and a server updates the activity through APNs.
start.push = Some;
live.start_layout.await?;
// What came before the sink listened.
for held in live.push_tokens.await?.activities
The server builds its pushes with liveactivity::push.
Content that a push delivered comes back as Event::Updated, and an activity
that a push started as Event::Started. The steps are in
docs/ios-setup.md.
Pushes from a server
The application sends its server what the device reported, as a Target:
The server makes one Message from the contract's own types and sends it with
the feature server. The key comes in as text; the crate reads no environment.
use ;
let key = from_pem?;
let account = from_json?;
let sender = builder
.apns
.fcm
.build?;
let message = update
.alert;
match sender.send.await
Message::start goes to the push-to-start token, update and end to an
activity's token or a channel; on Android all three go to the installation's FCM
token, and the device runs them as the application's own calls
(docs/android-setup.md). Without
server, message.request(&target, &application) gives the path, the headers
and the body for another HTTP client. The steps, from the key to what
came back, are in docs/server-push.md; the example
push_server is a server with axum.
Events
| Event | When | iOS | Android |
|---|---|---|---|
Event::Action |
a button, a toggle or an action was tapped; tap says which tap and when |
✓ also a tap the app took late | ✓ |
Event::State |
an activity has a new lifecycle state | every change while the app runs | the state it has when the backend is asked, once (active, stale, ended, dismissed) |
Event::Started |
a layout activity began, also one a push started | ✓ | |
Event::Updated |
a push changed the content of a layout activity | ✓ | |
Event::PushToken |
an activity got a push token, or a new one | ✓ | |
Event::PushToStartToken |
the app's push-to-start token, at launch and when it changes | ✓ | |
Event::Pushed |
a push from the server was taken or refused | ✓ with the feature android-push |
|
Event::Permissions |
the person changed what the app may do | ✓ | |
Event::Opened |
the person opened the application from a notification | the application gets the URL itself | ✓ when the host reports the opening intent |
Event::Unknown |
something this version has no event for | ✓ when the AAR is of a later liveupdate than the crate | |
Event::Handled |
a tap that the application's own component took | ✓ with an action's target |
A tap that the app did not take at once, because the tap launched it or its
process ended, comes to a later connection with tap.late (iOS); act on each
tap.id once:
// `handled` is the application's own set of tap ids, kept across launches.
if let Action = &event
The enum may grow: match with a _ arm. An activity is over when a State
event says ended or dismissed, on either platform:
/// The id of the activity that `event` says is over.
iOS delivers events as they come, and a tap keeps the app awake until the sink
returns, for at most 4 seconds. On Android nothing calls the application: it
asks. A start, an update, an end and a list report the states that are due
with them, and sync_events hands over what Android stored, the taps and the
openings, and the state of every activity that the sink was not told yet, such
as a stale time that has passed or a notification the person removed. The sink
is told where an activity stands, once, one event at a time and in order; a
state that came and went between two asks is not reported:
// When the application starts and resumes. On iOS it does nothing.
live.sync_events.await?;
Android tells nobody that a stale time has come. A host that has a thread and the default Android backend asks it when to ask again:
match backend.next_check?
Capabilities and permissions
let capabilities = live.capabilities.await?;
if capabilities.layout != Supported
let permissions = live.permissions.await?;
if permissions.notifications == Prompt
A feature is supported, unsupported or unknown, and unknown never counts as
supported. On iOS capabilities.ios says what the device runs of the later
features (push_to_start, push_channels, transient, scheduled_start,
small_family), so a request need not be tried to learn it. On Android
capabilities.android_presentation also says which
notification styles the device draws and which extensions it offers: links, a
header clock, a style that changes, a last content that stays, an update that
alerts, a public version and branding. On iOS Live Activities are a switch in
Settings and there is no dialog. On Android the system's dialog needs an Activity, which the host
shows: see Supporting it.
When Android says promotion: denied, a person's tap can open the settings of
the application's Live Updates. The two channels take the names that the
application gives the strings liveupdate_channel_name and
liveupdate_alert_channel_name (and their _description)
(docs/android-setup.md):
if permissions.promotion == Denied
Blocking calls
Calls are futures for any executor. Where there is none:
let record = wait?;
On iOS the answer comes from the main thread, so a blocking call there would
never end: wait answers NATIVE_ERROR at once on the main thread instead of
hanging. On Android a call runs on the thread that polls it.
Rules of every backend
- Key.
activity_keyis the application's own name of the activity. A repeated start with the same key and request answers the running activity; a different request under that key isCONFLICT. - Handle. Updates and ends take the
idof the record, new for every start. - Revision. A lower revision is
STALE_REVISION. The same revision with other content isCONFLICT; with the same content it changes nothing. - End. Ending twice is fine. An ended activity takes no update (
NOT_FOUND). - Errors. Every failure is an
Errorwith acodeout ofERROR_CODESand a message.SYSTEM_LIMITis the platform's limit: 32 live activities on Android, or no room left in liveupdate's store, and ActivityKit's on iOS. - Validation. Requests are checked against the contract before a backend
sees them, so an invalid one is
INVALID_INPUTon every platform. - What a platform leaves out is named in the record's
degraded_features.
Supporting it
A framework plugin
A plugin carries the contract over its framework and adds nothing to it.
examples/adapter.rs is a whole one
that compiles and runs, and
docs/framework-integration.md walks through
it.
-
Types. Re-export
liveactivity::contract::*: the requests and records are serde types with camelCase JSON, ready for the framework's IPC. -
Commands. Each command parses its input and awaits the call of the same name:
"start" => answer, -
Events. Give the builder a sink that forwards each event to the framework's own event API:
builder .event_sink .build?; -
Lifecycle. Build once at setup and keep the
LiveActivityin the framework's state. Callsync_events()at startup and on every resume. -
Backend. Name one only when the framework has a native side of its own. Wrap a default backend, answer what the framework does itself, and hand the rest on:
// The framework reaches Android through its own call: only the transport differs. let engine = over_bridge // my_bridge: liveupdate::Bridge .with_files; // The framework shows Android's permission dialog, which needs an Activity.
liveupdate::Bridge is one call that carries a command name and a JSON payload
to the Android side.
A platform
Implement LiveActivityBackend, one method per call of the contract. Only
the template calls and the two queries are required; layouts, permission
requests and images default to UNSUPPORTED_FEATURE, a backend without pushes
holds no token, and capabilities says what the backend has. attach hands
the backend the sink once, before any command; the backend sends each event
with events.emit(..).
The testkit feature checks that a backend keeps the rules above, which is what
makes it exchangeable with the default ones:
let live = builder.backend.build?;
conformance.await.assert_passed;
testkit::MemoryBackend is a backend without a platform, for an application's
own tests and as the smallest example of an implementation.
Platform setup
iOS. activitykit builds its Swift bridge into the Rust library, which needs
Xcode. The app needs NSSupportsLiveActivities, a widget extension that links
activitykit's ActivityKitWidget product, and an App Group for images. Live
Activities need iOS 16.1, or iPadOS 17 on an iPad. The steps are in
docs/ios-setup.md.
Android. The app packages liveupdate's AAR and the Kotlin standard library,
and the host initializes ndk-context, which common Rust Android runtimes do.
The default backend draws progress and timer activities as it is. Given the
folder of the app's files, it also draws layouts, stores images and reports
taps on buttons:
let backend = android?
.with_files; // Context.getFilesDir()
let live = builder.backend.build?;
Only the system's permission dialog needs code of the host, because it needs an Activity. The steps, and the 16 KB page-size note, are in docs/android-setup.md.
Develop
Requirements and their test evidence are in docs/requirements.md, the structure in docs/design.md.
Releases
Releases follow the subjects of the commits on main, which are
conventional commits. After CI passes
on main, the release workflow bumps the version, writes the changelog, waits
for CI on that commit, tags it and publishes the crate:
| Subject | Release |
|---|---|
feat: |
minor |
fix:, perf:, refactor:, docs:, revert: |
patch |
test:, ci:, chore:, build:, style: |
none |
! or a BREAKING CHANGE: footer |
major, only by running the "Auto release" workflow by hand with major checked |
License
MIT or Apache-2.0, at your option.