Get new pushToken from LiveActivity which created by push

When my app is in the background, I create a Live Activity through a push notification with token get from pushToStartTokenUpdates, and this process works fine. However, without opening the app, how can I retrieve the new push token for this Live Activity again and use it for subsequent updates to the Live Activity content?

Answered by Engineer in 818784022

Your Live Activity will be awoken in the background if you've implemented pushTokenUpdates. When the handler is called you can send the new token to your server.

Hopefully this helps.

Rico


WWDR | DTS | Software Engineer

@available(iOS 17.2, *)
    private static func getPushToken<Attributes>(attributes: Attributes) where Attributes : ActivityAttributes {
        
        Task{
            for try await pushtoken in Activity<Attributes>.pushToStartTokenUpdates{
                 var templateCode = ""
                if attributes is ZTPreOrderAttributes{
                    templateCode = ZTLiveActivityTemplatedCode.preOrder.rawValue
                } else if attributes is ZTSkillOrderAttributes{
                    templateCode = ZTLiveActivityTemplatedCode.skillOrder.rawValue
                }
                
                let pushtokenstr = pushtoken.map { String(format: "%02x", $0) }.joined()
//upload to server for create liveactivity by push
                uplaodToServer(pushtokenstr)
                               
                
            }
            
        }
        
        //get new token for update
        Task{
            for await activity in Activity<Attributes>.activityUpdates {
                ZTLiveActivityTool.log(" updateTokenForActivity 2")
                await updateTokenForActivity(activity)
            }
        }
    }

i call this code by applaunch ,Through testing, I found that if the app is not launched, and a Live Activity is created through a push notification which wakes up the app process, the push token for the Live Activity can be retrieved in Activity<Attributes>.activityUpdates and uploaded to the server for subsequent push updates.

However, when the app is in the background, the above code does not trigger any callbacks, and I cannot retrieve the latest push token.

Your Live Activity will be awoken in the background if you've implemented pushTokenUpdates. When the handler is called you can send the new token to your server.

Hopefully this helps.

Rico


WWDR | DTS | Software Engineer

Get new pushToken from LiveActivity which created by push
 
 
Q