How to create and update a Live Activity

How to set up Live Activities on your iOS Mobile App with OneSignal

In this tutorial, we walk you through setting up your application to use Live Activities and provide a code sample for entering a Live Activity.

This guide differs from our Push-To-Start Live Activities Guide in that it enables you to start a Live Activity remotely without users needing to be in the app for it to show.

Requirements

Activity ID

We've introduced an Activity ID in the Update Live Activity API to help you track your Live Activities and update multiple Live Activities that your users have started with a single API call.

For instance, if you want to update scores during a sports game, you can use Live Activities by assigning a unique Activity ID to each game. Whenever the score changes, use the API to update all Live Activities associated with that Activity ID, assuring that all users following the game receive the updates.
In some cases, Live Activities are user-specific. For example, you may use them to tell a user about the status of a food delivery order. In this case, you should assign a different Activity ID for each unique order.

OneSignal is designed to support Live Activities at scale, whether millions of users are following a single event (like a sports game) or millions of users following individual events (like food delivery status) that you want to update.

Send Your First Live Activity

1. Start a Live Activity

Import OneSignal and ActivityKit into your code and ensure the Activity's type is accessible from both targets.

import ActivityKit
import OneSignalFramework

Add code to begin a Live Activity and register the Activity ID with OneSignal.

import SwiftUI

import ActivityKit
import OneSignalFramework

struct ContentView: View {
    @StateObject private var viewModel = LiveActivityViewModel()

    var body: some View {
        VStack {
            Button("Start Live Activity") {
                viewModel.startLiveActivity()
            }
        }
    }
}


class LiveActivityViewModel: ObservableObject {
    func startLiveActivity() {
        let attributes = laLiveActivityAttributes(name: "OneSignal Dev App Live Activity")
        let contentState = laLiveActivityAttributes.ContentState(value:5)
        
        do {
            let activity = try Activity<laLiveActivityAttributes>.request(
                attributes: attributes,
                contentState: contentState,
                pushType: .token)
            
            Task {
                for await data in activity.pushTokenUpdates {
                    let myToken = data.map { String(format: "%02x", $0) }.joined()
                    OneSignal.LiveActivities.enter("my_activity_id", withToken: myToken)
                }
            }
        } catch {
            print(error.localizedDescription)
        }
    }
}
import UIKit

import ActivityKit
import OneSignalFramework

class ViewController: UIViewController {
    ///... ViewController code ...

    func startLiveActivity() {

        let attributes = OneSignalWidgetAttributes(name: "OneSignal Dev App Live Activity")
        let contentState = OneSignalWidgetAttributes.ContentState(value: 5)

        do {

            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()
                    OneSignal.LiveActivities.enter("my_activity_id", withToken: myToken)
                }
            }

        } catch (let error) {
            print(error.localizedDescription)
        }
    }
}

2. Update a Live Activity

Submit a request to the Update Live Activity API.

3. End a Live Activity

In addition to users ending a live activity by removing it from their home screen or revoking permissions for Live activities from their settings, you can programmatically end a Live Activity using our SDK.

Use the SDK

For Live Activities that were started by calling the enter method in the SDK.

  1. Remove the Live Activity from the user's home screen
  2. Send an Exit call using our OneSignal SDK
OneSignal.LiveActivities.exit("my_activity_id")  
OneSignal.exitLiveActivity("my_activity_id", (res: object) => {  
    loggingFunction("exitLiveActivity completed with result: ", JSON.stringify(res));  
})  
OneSignal.shared.exitLiveActivity("your_activity_id").then((v) {  
    print("Successfully exit live activity");  
}).catchError((error) {  
    print("Failed to exit live activity: $error");  
});  
window.plugins.OneSignal.exitLiveActivity("my_activity_id");  
OneSignalSDK.DotNet.OneSignal.Default.ExitLiveActivity("my_activity_id");  
OneSignalSDK.DotNet.OneSignal.Default.ExitLiveActivity("my_activity_id");  
OneSignal.Default.exitLiveActivity("my_activity_id");  

Use the API

For Live Activities started via pushToStart token

  1. The event parameter is set to "end"
  2. The dismissal_date is set to the time the Live Activity should disappear

What’s Next