How to get pushTokenUpdates when starting a Live Activity via Push Notification

I'm trying to build a Live Activity extension. I can successfully start my live activity via push notification. The problem is, when I start live activity from my app, I can get pushTokenUpdates since I control everything and run the for loop that gets pushTokenUpdates. But the code that gets pushTokenUpdates isn't called when I start live activity with push notification since system starts it automatically (maybe it is called, but I don't know when and where).

Where am I supposed to get pushTokenUpdates when I start Live Activity using push notification to send them to my server?

The relevant code is below:

for await data in activity.pushTokenUpdates {
    let token = data.map { String(format: "%02x", $0) }.joined()
     Logger().log("Activity token: \(token)")
     // send token to the server
}

I have this problem too! There's no update token after the live activity starts, so we can't update and end the started live activity without the update token. How we can solve it? 😭

Same problem here!

I'm using silent push notifications to start the Live Activities using the pushToStartTokenUpdates from my Live Activity class, which is working nice. The issue is that I'm not able to receive the pushTokenUpdates and send it to the backend.

  1. I'm registering the device pushToStartTokenUpdates and sending it to the backend on the application(_:didFinishLaunchingWithOptions:) on the AppDelegate.
            for await data in Activity<GiveawayWidgetAttributes>.pushToStartTokenUpdates {
                let pushToStartToken = data.map { String(format: "%02x", $0) }.joined()
                print("SEND PUSH TO START TOKEN TO THE BACKEND: \(pushToStartToken)")
            }
        }
  1. I'm processing the remote notifications on the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

  2. I'm validating and analyzing the aps payload to know which kind of event it means. Start, update or end are the 3 possibilities

  3. I'm starting the Live Activity by doing this:

do {
    activity = try Activity<GiveawayWidgetAttributes>.request(
         attributes: attributes,
         content: initialContent,
         pushType: .token
    )
} catch { 
    print("ERROR: \(error.localizedDescription)")
}
        
Task {
    for await pushToken in activity!.pushTokenUpdates {
         let pushToken = pushToken.reduce("") { $0 + String(format: "%02x", $1) }
         print("SEND PUSH TOKEN UPDATES: \(pushToken)")
         // THIS IS NEVER CALLED
    }
}

On my Info.plist:

  • Supports Live Activities = TRUE
  • Supports Live Activities Frequent Updates = TRUE

I can't even debug the code, because it seems that the app runs on background when receiving a silent push notification.

Possible solutions that I thought:

  • Perform a background task to send the pushTokenUpdates to the backend
  • Use URLSession with background configuration

Does anyone knows if this is even possible? For me it seems like a very important thing in order to use the pushToStartTokenUpdates while using a back end to trigger the push notifications to update or end.

Just found out a solution

You can add this code on your AppDelegate to receive updates from a live activity. I hope it works for you!

Task {
   for await activity in Activity<GiveawayWidgetAttributes>.activityUpdates {

      Task {
         for await tokenData in activity.pushTokenUpdates {
            let token = tokenData.map {String(format: "%02x", $0)}.joined()
            print("Activity ID:\(activity.id) Push token: \(token)")
            // SEND TO  BACKEND     
          }
       }

       Task {
          for await state in activity.activityStateUpdates {
             print("Observer Activity:\(activity.id) state:\(state)")
          }
       }

   }
}
How to get pushTokenUpdates when starting a Live Activity via Push Notification
 
 
Q