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
}
Post
Replies
Boosts
Views
Activity
I'm trying to develop a Live Activity Extension. The problem is, I can't get pushToStartToken. I'm able to get it when I start a Live Activity, but I can't when I don't start a Live Activity.
This function successfully generates the token:
private func startNewLiveActivity() async {
guard #available(iOS 16.2, *) else { return }
let attributes = MyWidgetAttributes(
homeTeam: "Badger",
awayTeam: "Lion",
date: "12/09/2023"
)
let initialContentState = ActivityContent(
state: MyWidgetAttributes.ContentState(
homeTeamScore: 0,
awayTeamScore: 0,
lastEvent: "Match Start"
),
staleDate: nil
)
guard let activity = try? Activity.request(
attributes: attributes,
content: initialContentState,
pushType: .token
) else { return }
if #available(iOS 17.2, *) {
Task {
for await data in Activity< MyWidgetAttributes>.pushToStartTokenUpdates {
let token = data.map { String(format: "%02x", $0) }.joined()
// THE DESIRED pushToStartToken TOKEN IS GENERATED HERE
}
}
}
for await data in activity.pushTokenUpdates {
let token = data.map { String(format: "%02x", $0) }.joined()
// I send token to server here.
}
}
But when I try to get pushToStartToken separately, without creating a live activity, it doesn't return any value:
private func getPushToStartToken() async {
guard #available(iOS 17.2, *) else { return }
Task {
for await data in Activity<MyWidgetAttributes>.pushToStartTokenUpdates {
let token = data.map { String(format: "%02x", $0) }.joined()
// THIS DOESN'T GENERATE ANY TOKENS SINCE THE ACTIVITY IS NOT CREATED
}
}
}