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)")
}
}
}
}
Post
Replies
Boosts
Views
Activity
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.
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)")
}
}
I'm processing the remote notifications on the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
I'm validating and analyzing the aps payload to know which kind of event it means. Start, update or end are the 3 possibilities
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.