Live Activities

A quick start guide to adding Live Activity support to your iOS application with the Pushly PushSDK

Live Activities, introduced by Apple in October 2022, allow app developers to display dynamic, live-updating, content via widgets on device lock screens and in the Dynamic Island (for capable devices).

The PushSDK helps you streamline Live Activity registration and updates by allowing developers to register and manage the short-lived push tokens associated with your Live Activities which can then be updated via our server API.

Prerequisites

Before you can integrate Live Activities with the SDK the following requirements must be met:

  • An iOS Send Integration configured with a .p8 key.

  • An iOS app (Live Activities are only available for iOS and iPadOS).

  • A device or emulator with iOS 16.1 or newer installed

  • PushSDK release supporting Live Activities (See version support chart below)

Step 1: iOS SDK Setup

For applications not already using the PushSDK please see our Native App Push setup guide for Apple / iOS.

For applications already using the PushSDK you might need to upgrade to a later version that includes Live Activity support. Use the following table to determine if you need to upgrade your installation.

SDKEarliest Version with Live Activities

1.2.0+

1.1.0+

1.1.0+

Within the Xcode project navigator panel locate Package Dependencies. Locate and right-click on Pushly and select Update Package.

If after an update the package is not >= version 1.2.0 select your Project in the project navigator, select your project, and then navigate to Package Dependencies. Select Pushly and ensure the Dependency Rule is set to Up to Next Major Version with 1.0.0, or greater, as the target.

Step 2: Add Live Activity Support to Info.plist

Applications implementing Live Activities must set the Supports Live Activities key to YES in their primary target info.plist.

Step 3: Add a Live Activity Widget Extension

Within your app's Xcode project select File > New > Target. Select Widget Extension inside the iOS templates tab and click Next.

Enter your desired Live Activity widget name for the Product Name and any other configuration details for your widget extension, making sure that Include Live Activity is selected, and then click Finish. On the subsequent dialog click Cancel to continue developing without activating the new widget extension as the current target.

Step 4: Setup a Live Activity and Register with Pushly

User Interface

Before starting your new Live Activity we recommend reading through Apple's guide to Displaying live data with Live Activities.

Starting Your Live Activity

Once your Live Activity has been designed and setup you are now ready to start and register it via the PushSDK.

To aid in tracking and updating multiple Live Activity instances across millions of devices via a single API call we use an activity ID. Activity IDs are string values provided by you, the developer.

In some scenarios, such as sporting events, you may want to send a single update to all devices registered to a single event. In these cases you should use a unique ID that identifies the event rather than an ID per individual user.

In other scenarios, such as meal orders, Live Activities are user specific. In these cases you should use a unique ID that identifies the individual user.

Add Code to Start Your Live Activity

The following code snippet requests to start a Live Activity, name "My Live Activity" with an ID of "my_activity_id", and then listens for token updates to register them with the PushSDK.

import UIKit
import ActivityKit
import Pushly

class ViewController: UIViewController {
    public func startLiveActivity() {
        if #available(iOS 16.1, *) {
            let attributes = MyLiveActivityAttributes(name: "My Live Activity")
            let contentState = MyLiveActivityAttributes.ContentState(value: 1)
        
            do {
                let activity = try Activity.request(
                    attributes: attributes,
                    contentState: .init(state: contentState, staleDate: nil),
                    pushType: .token)
            
                Task {
                    for await tokenData in activity.pushTokenUpdates {
                        let token = tokenData.reduce("") {
                            $0 + String(format: "%02.2hhx", $1)
                        }

                        // Register the short-lived token with Pushly 
                        PushSDK.LiveActivities.register(
                            token: token,
                            forActivity: "my_activity_id")
                    }
                }
            } catch (let error) {
                print(error.localizedDescription)
            }
        }
    }
}

For more information on Starting and updating Live Activities with ActivityKit push notifications see Apple's developer documentation.

Step 5: Send Your Live Activity an Update

Once short-lived tokens have been registered with an activity ID you can use our Live Activity API documentation to send updates to all registered Live Activities via a single request.

Last updated