Skip to content

Commit 2b99653

Browse files
taitala-mlbnan-li
authored andcommitted
fix(live activities): Register push tokens in more places
* Apple has confirmed that when using push-to-start, it is best to check both Activity<...>.pushToken in addition to Activity<...>.pushTokenUpdates --- as well as checking Activity<...>.activities in addition to Activity<...>.activityUpdates --- because your app may need to launch in the background and the launch time may end up being slower than the new values come in. In those cases, your task on the update sequence may start listening after the initial values were already provided.
1 parent f518552 commit 2b99653

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

iOS_SDK/OneSignalSDK/OneSignalLiveActivities/Source/OneSignalLiveActivitiesManagerImpl.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ public class OneSignalLiveActivitiesManagerImpl: NSObject, OSLiveActivities {
190190

191191
@available(iOS 16.1, *)
192192
private static func listenForActivity<Attributes: OneSignalLiveActivityAttributes>(_ activityType: Attributes.Type, options: LiveActivitySetupOptions? = nil) {
193+
194+
/*
195+
Apple has confirmed that when using push-to-start, it is best to check both `Activity<...>.activities` in addition
196+
`Activity<...>.activityUpdates` --- because your app may need to launch in the background and the launch time may end
197+
up being slower than the new values come in. In those cases, your task on the update sequence may start listening after
198+
the initial values were already provided.
199+
*/
200+
201+
// Establish listeners for activity (if any exist)
202+
for activity in Activity<Attributes>.activities {
203+
listenForActivityStateUpdates(activityType, activity: activity, options: options)
204+
listenForActivityPushToUpdate(activityType, activity: activity, options: options)
205+
}
206+
207+
// Establish listeners for activity updates
193208
Task {
194209
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities listening for activity on: \(activityType)")
195210
for await activity in Activity<Attributes>.activityUpdates {
@@ -198,6 +213,7 @@ public class OneSignalLiveActivitiesManagerImpl: NSObject, OSLiveActivities {
198213
// listening for the new activity's events.
199214
for otherActivity in Activity<Attributes>.activities {
200215
if activity.id != otherActivity.id && otherActivity.attributes.onesignal.activityId == activity.attributes.onesignal.activityId {
216+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "OneSignal.LiveActivities dismissing other activity: \(activityType):\(otherActivity.attributes.onesignal.activityId):\(otherActivity.id)")
201217
await otherActivity.end(nil, dismissalPolicy: ActivityUIDismissalPolicy.immediate)
202218
}
203219
}
@@ -230,10 +246,26 @@ public class OneSignalLiveActivitiesManagerImpl: NSObject, OSLiveActivities {
230246
@available(iOS 16.1, *)
231247
private static func listenForActivityPushToUpdate<Attributes: OneSignalLiveActivityAttributes>(_ activityType: Attributes.Type, activity: Activity<Attributes>, options: LiveActivitySetupOptions? = nil) {
232248
if options == nil || options!.enablePushToUpdate {
249+
250+
/*
251+
Apple has confirmed that when using push-to-start, it is best to check both `Activity<...>.pushToken` in addition
252+
`Activity<...>.pushTokenUpdates` --- because your app may need to launch in the background and the launch time may end
253+
up being slower than the new values come in. In those cases, your task on the update sequence may start listening after
254+
the initial values were already provided.
255+
*/
256+
257+
// Set the initial pushToken (if one exists)
258+
if let pushToken = activity.pushToken {
259+
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities enter with existing pushToken for: \(activityType):\(activity.attributes.onesignal.activityId):\(activity.id)")
260+
let token = pushToken.map {String(format: "%02x", $0)}.joined()
261+
OneSignalLiveActivitiesManagerImpl.enter(activity.attributes.onesignal.activityId, withToken: token)
262+
}
263+
233264
// listen for activity update token updates so we can tell OneSignal how to update the activity
234265
Task {
235266
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities listening for pushToUpdate on: \(activityType):\(activity.attributes.onesignal.activityId):\(activity.id)")
236267
for await pushToken in activity.pushTokenUpdates {
268+
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities pushTokenUpdates observed for: \(activityType):\(activity.attributes.onesignal.activityId):\(activity.id)")
237269
let token = pushToken.map {String(format: "%02x", $0)}.joined()
238270
OneSignalLiveActivitiesManagerImpl.enter(activity.attributes.onesignal.activityId, withToken: token)
239271
}

0 commit comments

Comments
 (0)