We are adding a live activity to our app that is started by a push to start live activity token that we supply to our server backend. In the app I have a Task that is retrieving pushToStartTokens from the asynchronous stream provided by the Apple API
It looks similar to:
// Iterate the async stream from the system
for await tokenData in try await Activity<MyActivityAttributes>.pushToStartTokenUpdates {
let tokenString = tokenData.map { String(format: "%02x", $0) }.joined()
logger.log("Received push start token: \(tokenString, privacy: .public)")
}
} catch {
logger.error("Failed to monitor push start tokens: \(error.localizedDescription, privacy: .public)")
}
When my app is launched from Xcode and connected via the debugger this code vends a pushToStartToken reliably. However if I run this same code by directly launching the app by tapping the icon on the phone, it almost never vends a pushToStartToken. It only occasionally works. I've tried a variation on the code where instead of always executing the asynchronous stream to obtain the token it first checks for the existence of a pushToStartToken using the this synchronous check prior to entering the for await
if let pushStartTokenSync = Activity<AttributeType>.pushToStartToken {
let tokenStr = pushStartToekSync.map { String(format: "%02x", $0) }.joined()
nextPushToStartToken = pushStartTokenSync
logger..log("**** Queried PushToStart Token: \(tokenStr, privacy: .public) ***")
} else {
logger..log("**** Queried PushToStart Token is nil! ***")
}
This works more reliably than just falling directly into the stream but I still see many instances where the result is nil. I'm trying to understand what is the correct way to obtain and manage the pushToStartTokens so that getting one is as reliable as possible especially in production builds. When I do get a token, should I persist it somewhere and use that (even across different app executions) until a new one is vended?
Appreciate hearing ideas, thoughts and any code samples that illustrate a good management scheme
Thank, You.
Rob S.