Live Activities SDK Methods

Live Activities are a type of interactive push notification. They were designed by Apple to enable iOS apps to provide real-time updates to their users that are visible from the lock screen and dynamic island.

Live Activity support comes with OneSignal iOS SDK version 3.12.3 or above.

Permission

Live Activities do not need explicit user approval. A developer may start and update a Live Activity via any method without an explicit prompt (unlike Notification Permission or Location Permission). Live Activities appear with the iOS Provisional Authorization UI.

Developer Guidelines

As with any iOS development, we recommend Apple’s developer guidelines.

According to Apple’s guidelines, applications should give users the option to opt-in to Live Activities. For example, an app can give a user an option to start a Live Activity through it’s UX, such as as a button or in-app. Live Activities must be started when the application is in the foreground.

Enter a Live Activity

Entering a Live Activity associates an activityId with a live activity temporary push token on OneSignal's server. The activityId is then used with the OneSignal REST API to update one or multiple Live Activities at one time.

ParameterTypeDescription
activityIdStringA customer defined identifier that gets associated with the temporary push token on OneSignal's server. When the customer uses the update endpoint they will send this activityId to specify which push tokens they would like to update.

NOTE: activityId cannot contain any / (forward slash) characters.
tokenStringThe Live Activity temporary push token from Apple.

enterliveActivity usage with context:

// ... your app's code 
let activity = try Activity<OneSignalWidgetAttributes>.request(
  attributes: attributes,
  contentState: contentState,
  pushType: .token)
Task {
    for await data in activity.pushTokenUpdates {
        let myToken = data.map {String(format: "%02x", $0)}.joined()
        // ... required code for entering a live activity
        OneSignal.LiveActivities.enter("my_activity_id", withToken: myToken)
    }
}
OneSignal.LiveActivities.enter("my_activity_id", token, (result: object) => {
    loggingFunction("enterLiveActivity completed with result: ", JSON.stringify(result));
})
OneSignal.LiveActivities.enterLiveActivity("my_activity_id", token).then((result) {
    print("Successfully enter live activity");
}).catchError((error) {
    print("Failed to enter live activity with error: $error");
});
var result = OneSignalSDK.DotNet.OneSignal.Default.EnterLiveActivity("my_activity_id", token);
if(result) {
    Console.WriteLine("Success");
}
var result = OneSignalSDK.Xamarin.OneSignal.Default.EnterLiveActivity("my_activity_id", token);
if(result) {
    Console.WriteLine("Success");
}
OneSignal.LiveActivities.EnterAsync("my_activity_id", token);
window.plugins.OneSignal.LiveActivities.enter("ACTIVITY_ID", "TOKEN", (result) => {
  console.log("Results of entering live activity: ", result);
});
OneSignal.LiveActivities.enter('ACTIVITY_ID', 'TOKEN', (result) => {
  console.log('Results of entering live activity: ', result);
});

Exit a Live Activity

Exiting a Live activity deletes the association between a customer defined activityId with a Live Activity temporary push token on OneSignal's server.

exitliveActivity usage

OneSignal.LiveActivities.exit("my_activity_id")
OneSignal.LiveActivities.exit("my_activity_id", (result: object) => {
    loggingFunction("exitLiveActivity completed with result: ", JSON.stringify(result));
})
OneSignal.LiveActivities.exit("your_activity_id").then((result) {
    print("Successfully exit live activity");
}).catchError((error) {
    print("Failed to exit live activity: $error");
});
OneSignalSDK.DotNet.OneSignal.Default.ExitLiveActivity("my_activity_id");
OneSignalSDK.DotNet.OneSignal.Default.ExitLiveActivity("my_activity_id");
OneSignal.LiveActivities.ExitAsync("my_activity_id");
window.plugins.OneSignal.LiveActivities.exit("ACTIVITY_ID", (result) => {
  console.log("Results of exiting live activity: ", result);
});
OneSignal.LiveActivities.exit('ACTIVITY_ID', (result) => {
  console.log('Results of exiting live activity: ', result);
});