@@ -96,6 +96,7 @@ const registeredActivityPushTokens = new Map<string, number>();
9696let widgetSnapshotReads = 0 ;
9797let androidDeviceReplayedAt : number | null = null ;
9898let lastNativePushToken : string | null = null ;
99+ let nativePushTokenRotations = 0 ;
99100let pushTokenSubscription : { remove : ( ) => void } | null = null ;
100101let appStateSubscription : { remove : ( ) => void } | null = null ;
101102
@@ -293,12 +294,14 @@ const nativePushTokenRegistration = Effect.fn("remoteRegistration.nativePushToke
293294 : true ) ;
294295 if ( observedPushToken ) {
295296 lastNativePushToken = observedPushToken ;
297+ nativePushTokenRotations ++ ;
296298 return {
297299 notificationsEnabled : alertsEnabled ,
298300 pushToken : observedPushToken ,
299301 notificationPermissionGranted : permissions . granted ,
300302 } ;
301303 }
304+ const rotationsBeforeLookup = nativePushTokenRotations ;
302305 const token = yield * Effect . tryPromise ( {
303306 try : ( ) => Notifications . getDevicePushTokenAsync ( ) ,
304307 catch : ( cause ) =>
@@ -314,11 +317,15 @@ const nativePushTokenRegistration = Effect.fn("remoteRegistration.nativePushToke
314317 ) ,
315318 Effect . orElseSucceed ( ( ) => null ) ,
316319 ) ;
317- const pushToken =
320+ const lookedUpPushToken =
318321 token ?. type === Platform . OS && typeof token . data === "string" && token . data . trim ( ) . length > 0
319322 ? token . data . trim ( )
320- : lastNativePushToken ;
321- lastNativePushToken = pushToken ;
323+ : null ;
324+ // A rotation observed while this lookup ran is newer than what the lookup returned.
325+ if ( lookedUpPushToken && rotationsBeforeLookup === nativePushTokenRotations ) {
326+ lastNativePushToken = lookedUpPushToken ;
327+ }
328+ const pushToken = lastNativePushToken ;
322329 // A missing token must not disable notifications the relay accepted.
323330 return {
324331 notificationsEnabled : alertsEnabled ,
@@ -868,6 +875,7 @@ export function __resetAgentAwarenessRemoteRegistrationForTest(): void {
868875 registrationStatus = "unknown" ;
869876 androidDeviceReplayedAt = null ;
870877 lastNativePushToken = null ;
878+ nativePushTokenRotations = 0 ;
871879 registrationStatusListeners . clear ( ) ;
872880 registeredActivityPushTokens . clear ( ) ;
873881 activityPushTokenLocks . clear ( ) ;
0 commit comments