@hassankbrian/capacitor-live-activity
v7.5.0
Published
A Capacitor plugin for managing iOS Live Activities using ActivityKit and Swift.
Maintainers
Readme
📡 capacitor-live-activity
A Capacitor plugin for managing iOS Live Activities using ActivityKit and Swift.
[!TIP] 🚀 Looking for a ready-to-run demo? → Try the Example App
🧭 Table of contents
startActivity(...)startSingleActivity(...)startOrUpdateActivity(...)updateActivity(...)endActivity(...)isAvailable()isRunning(...)getCurrentActivity(...)getAllActivities()endAllActivities()detectDuplicates()endActivitiesById(...)- Interfaces
- Type Aliases
📦 Install
npm install capacitor-live-activity
npx cap sync[!NOTE] This plugin requires iOS 16.2+ to work properly due to
ActivityKitAPI usage.
[!IMPORTANT] This plugin requires a Live Activity widget extension to be present and configured in your Xcode project.
Without a widget, Live Activities will not appear on the lock screen or Dynamic Island.
🧩 Widget Setup (Required)
To use Live Activities, your app must include a widget extension that defines the UI for the Live Activity using ActivityKit. Without this, the Live Activity will not appear on the Lock Screen or Dynamic Island.
1. Add a Widget Extension in Xcode
- Open your app’s iOS project in Xcode.
- Go to File > New > Target…
- Choose Widget Extension.
- Name it e.g. LiveActivityWidget.
- Check the box “Include Live Activity”.
- Finish and wait for Xcode to generate the files.
2. Configure the Widget (Example)
Make sure the widget uses the same attribute type as the plugin (e.g. GenericAttributes.swift):
import ActivityKit
import WidgetKit
import SwiftUI
struct LiveActivityWidgetLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: GenericAttributes.self) { context in
// Lock Screen UI
VStack {
Text(context.state.values["title"] ?? "⏱")
Text(context.state.values["status"] ?? "-")
}
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
Text(context.state.values["title"] ?? "")
}
DynamicIslandExpandedRegion(.trailing) {
Text(context.state.values["status"] ?? "")
}
DynamicIslandExpandedRegion(.bottom) {
Text(context.state.values["message"] ?? "")
}
} compactLeading: {
Text("🔔")
} compactTrailing: {
Text(context.state.values["status"] ?? "")
} minimal: {
Text("🎯")
}
}
}
}3. Add GenericAttributes.swift to your Widget Target
To support Live Activities with dynamic values, this plugin uses a shared Swift struct called GenericAttributes.
By default, it’s located under: Pods > CapacitorLiveActivity > Shared > GenericAttributes.swift
To make it available in your widget extension:
- Open Xcode and go to the File Navigator.
- Expand Pods > CapacitorLiveActivity > Shared.
- Copy GenericAttributes.swift to Widget Extension Target, e.g. LiveActivityWidget
- Make sure to select "Copy files to destination"
Why is this needed?
Xcode doesn’t automatically include files from a CocoaPods plugin into your widget target. Without this step, your widget won’t compile because it cannot find GenericAttributes.
4. Add Capability
Go to your main app target → Signing & Capabilities tab and add:
- Background Modes → Background fetch
5. Ensure Inclusion in Build
- In your App target’s Info.plist, ensure:
<key>NSSupportsLiveActivities</key>
<true/>- Clean and rebuild the project (Cmd + Shift + K, then Cmd + B).
📱 Example App
This plugin includes a fully functional demo app under the example-app/ directory.
The demo is designed to run on real iOS devices and showcases multiple Live Activity types like delivery, timer, taxi, workout, and more.
- Launch and test various Live Activities interactively
- Trigger updates and alert banners
- View JSON state changes in a live log console
[!NOTE] For full instructions, see example-app/README.md
🛠 API
startActivity(...)
startActivity(options: StartActivityOptions) => Promise<void>Starts a new Live Activity on iOS using the provided options.
| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| options | StartActivityOptions |
Since: 0.0.1
startSingleActivity(...)
startSingleActivity(options: StartActivityOptions) => Promise<void>Starts a single Live Activity, ensuring no duplicates exist.
This method enforces that only ONE live activity exists at a time by ending all existing activities before creating the new one. Perfect for meal tracking apps that should never have multiple activities running simultaneously.
| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| options | StartActivityOptions |
Since: 7.2.0
startOrUpdateActivity(...)
startOrUpdateActivity(options: StartActivityOptions) => Promise<void>Starts a Live Activity or updates it if it already exists.
This method atomically checks if a Live Activity with the given ID already exists. If it does, it updates the existing activity. If it doesn't exist, it creates a new one. This eliminates race conditions that can cause duplicate activities.
| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| options | StartActivityOptions |
Since: 7.5.0
updateActivity(...)
updateActivity(options: UpdateActivityOptions) => Promise<void>Updates the currently active Live Activity.
| Param | Type |
| ------------- | ----------------------------------------------------------------------- |
| options | UpdateActivityOptions |
Since: 0.0.1
endActivity(...)
endActivity(options: EndActivityOptions) => Promise<void>Ends the Live Activity and optionally provides a final state and dismissal policy.
| Param | Type |
| ------------- | ----------------------------------------------------------------- |
| options | EndActivityOptions |
Since: 0.0.1
isAvailable()
isAvailable() => Promise<boolean>Returns whether Live Activities are available on this device and allowed by the user.
Returns: Promise<boolean>
Since: 0.0.1
isRunning(...)
isRunning(options: { id: string; }) => Promise<boolean>Returns true if a Live Activity with the given ID is currently running.
| Param | Type |
| ------------- | ---------------------------- |
| options | { id: string; } |
Returns: Promise<boolean>
Since: 0.0.1
getCurrentActivity(...)
getCurrentActivity(options?: { id?: string | undefined; } | undefined) => Promise<LiveActivityState | undefined>Returns the current active Live Activity state, if any.
If an ID is provided, returns that specific activity. If no ID is given, returns the most recently started activity.
| Param | Type |
| ------------- | ----------------------------- |
| options | { id?: string; } |
Returns: Promise<LiveActivityState>
Since: 0.0.1
getAllActivities()
getAllActivities() => Promise<LiveActivityInfo[]>Returns all currently active Live Activities.
Useful for detecting duplicates and managing multiple activities.
Returns: Promise<LiveActivityInfo[]>
Since: 0.1.0
endAllActivities()
endAllActivities() => Promise<void>Ends ALL currently active Live Activities.
This is the nuclear option for cleaning up duplicates and starting fresh. Use with caution as it will end activities from other parts of your app.
Since: 0.1.0
detectDuplicates()
detectDuplicates() => Promise<DuplicateActivityInfo[]>Detects and returns duplicate Live Activities with the same ID.
Returns information about activities that share IDs, which shouldn't happen in a properly functioning system.
Returns: Promise<DuplicateActivityInfo[]>
Since: 0.1.0
endActivitiesById(...)
endActivitiesById(options: { id: string; }) => Promise<{ endedCount: number; }>Ends all activities matching a specific ID.
Unlike endActivity which targets one activity, this ensures ALL instances of an ID are cleaned up, solving duplicate issues.
| Param | Type |
| ------------- | ---------------------------- |
| options | { id: string; } |
Returns: Promise<{ endedCount: number; }>
Since: 0.1.0
Interfaces
StartActivityOptions
Options for starting a Live Activity.
| Prop | Type | Description |
| ------------------ | --------------------------------------------------------------- | --------------------------------------------------------- |
| id | string | Unique ID to identify the Live Activity. |
| attributes | Record<string, string> | Immutable attributes that are part of the Live Activity. |
| contentState | Record<string, string> | Initial content state (dynamic values). |
| timestamp | number | Optional timestamp (Unix) when the Live Activity started. |
UpdateActivityOptions
Options for updating a Live Activity.
| Prop | Type | Description |
| ------------------ | ----------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| id | string | ID of the Live Activity to update. |
| contentState | Record<string, string> | Updated content state (dynamic values). |
| alert | AlertConfiguration | Optional alert configuration to show a notification banner or Apple Watch alert. |
| timestamp | number | Optional timestamp (Unix) when the update occurred. |
AlertConfiguration
Configuration for alert notifications.
| Prop | Type | Description |
| ----------- | ------------------- | -------------------------------------- |
| title | string | Optional title of the alert. |
| body | string | Optional body text of the alert. |
| sound | string | Optional sound file name or "default". |
EndActivityOptions
Options for ending a Live Activity.
| Prop | Type | Description |
| ------------------- | --------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| id | string | ID of the Live Activity to end. |
| contentState | Record<string, string> | Final state to show before dismissal. |
| timestamp | number | Optional timestamp (Unix) when the end occurred. |
| dismissalDate | number | Optional dismissal time in the future (Unix). If not provided, system default applies. |
LiveActivityState
Represents an active Live Activity state.
| Prop | Type | Description |
| --------------- | --------------------------------------------------------------- | ----------------------------------------------- |
| id | string | The unique identifier of the activity. |
| values | Record<string, string> | The current dynamic values of the activity. |
| isStale | boolean | Whether the activity is stale. |
| isEnded | boolean | Whether the activity has ended. |
| startedAt | string | ISO string timestamp when the activity started. |
LiveActivityInfo
Basic info about a Live Activity for management purposes.
| Prop | Type | Description |
| --------------- | ---------------------------------------------------------- | -------------------------------------------------------------------- |
| id | string | The unique identifier of the activity. |
| state | 'active' | 'stale' | 'ended' | 'dismissed' | The current state of the activity. |
| isActive | boolean | Whether the activity is currently active or stale (i.e., not ended). |
| startedAt | string | ISO string timestamp when the activity started. |
DuplicateActivityInfo
Information about duplicate activities detected in the system.
| Prop | Type | Description |
| ---------------- | ------------------------------- | ---------------------------------------- |
| id | string | The ID that has duplicates. |
| count | number | Number of activities found with this ID. |
| activities | LiveActivityInfo[] | Details of each duplicate activity. |
Type Aliases
Record
Construct a type with a set of properties K of type T
{ [P in K]: T; }
