ActivityKit allows you to display live activities to show status and updates
Preparations
- You should understand how push notifications work. Your App can request a push token (APNS) so you can send it to a server before you can send push tokens. APNS then sends the payload to the device.
- There is a new APNS live activity push type – only available with Token-based connection based apps.
- Modify your app so it can handle push notifications – add the capability in Signing and Capabilities in Xcode
- Requesting a Push Token is an asynchronous process, so you can’t request one immediately after registering your app with the pushType: .token in the above code. The proper way to handles is to setup a Task that awaits for the appropriate activity, etc.
- In the above code we convert to Hexadecimal string for debugging purposes
First push update
- This is an http request to APNs – you must provide the following headers
- apns-push-type: live activity
- apns-topic: <BUNDLE_ID>.push-type.liveactivity
- apns-priority: 5 this is low priority, high priority is 10
- Your payload is a package with appropriate values. In the ranger app it looks like this
{
“aps”: {
“timestamp”: 1685952000,
“event”: “update”,
“content-state”: {
“currentHealthLevel”: 0.941,
“eventDescription”: “Power Panda found a sword!”
}
}
}
- Note the timestamp is seconds since 1970, event is the action for the live activity – it should be update or end, content-state is the json package that the live activity to decide with data to be used / displayed.
- Don’t set any custom encoding Strategies for your json – or the system may not function correctly
- You can use terminal to send info to APNS without requiring changes to your server – This is covered in the developer documentation
- If you use terminal grab the push token from the log in the step above and set an environment variable of ACTIVITY_PUSH_TOKEN with the value
- Now if you use the terminal to test your push notification it will have the correct token
- Here’s a sample CURL command
- Note you still need your authentication Token for the API based on our brearer token. And make sure you are using http2 to access the bearer token
- To debug look at device logs in Console app look at the following processes – liveactivitiesd, apsd, and chronod
Priority and alerts
- Make sure you use the correct priority, start by using low priority – it is opportunistic and saves the users battery
- There is no limit to low priority updates
- High priority are delivered immediately, and there is a budget based on the device condition.
- If you have a frequent update requirement there is a “Requires frequent high-priority Updates” feature for apps, which get’s a higher update budget, but may still be throttled
- Just add a new a new key to info.plist of NSSupportsLiveActivitiesFrequentUpdates=YES
- Note users can disable this – so check the .frequentPushesEnabled property in the ActivityAuthorizationInfo()
- You an add an alert object to a push notification – with a title, body, and sound – note you should localize this message via a localized string object.This will generate an alert on the device. To add custom sounds you must have it as an app resource and you can then set that in the payload.
Enhancements
- Sending a push payload of “event”: “end” – you can then add a custom dismissal-date (again in seconds since 1970).
- You can add a “stale-date” to allow a view to react to users missing an update – then update your UI to change state based on it.