Skip to content

fix: [SDK-5580] end duplicate Live Activities and keep the update token - #1780

Merged
nan-li merged 5 commits into
mainfrom
nan/sdk-5580
Dec 1, 2026
Merged

nan-li merged 5 commits into
mainfrom
nan/sdk-5580

Conversation

@nan-li

@nan-li nan-li commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

One Line Summary

End duplicate Live Activities at startup and keep their shared update token when one is dismissed. SDK-5580.

Details

Motivation

When two subscriptions on one device hold the same push-to-start token, one push starts two Live Activities with the same OneSignal activityId. They share one update token registration, so only the activity registered last receives updates, and dismissing either one removed the registration and froze the other.

Scope

  • When an activity is dismissed, the SDK checks for another live activity of the same type with the same activityId. If one exists, the SDK does not call exit. With enablePushToUpdate on, it also registers the token of a live duplicate that has one. The dismissed activity can hold the registration, because the last enter for an activityId wins.
  • In setup(), live activities that share an activityId are reduced to one. The SDK keeps the activity whose push token matches the cached update token, otherwise the first one with a token, ends the rest immediately, and attaches listeners only to the kept one. If the kept activity ends during the dedupe, the SDK picks another live one. If none stays live, it calls exit for that activityId. Needs iOS 16.2, like the existing dedupe of newly started activities.
  • The activityUpdates loop now waits for the setup dedupe before it handles a yielded activity, and it skips an activity that is no longer live. The two dedupes keep different activities (cached token versus newest), so if both run at once they can end every duplicate. The liveness check also stops the existing loop from ending both activities when the stream yields one that the loop already ended.
  • Nothing changes for apps that do not call setup(). For apps that do, every change above applies with enablePushToUpdate on or off, except registering a duplicate's token on dismissal. For activities without a duplicate, the only change is that the activityUpdates loop no longer attaches listeners to an activity that has already ended, on any iOS version.

Two cases are left as they are, because each needs several unlikely things at once:

  • A dismissed activity's token can stay registered. This needs a dismissal while a duplicate is live, and then no enter afterwards, which means enablePushToUpdate is off or the remaining activity never receives a token. The remaining widget is not affected, and exit runs when it is dismissed.
  • A second setup() call for the same activity type can end every copy. The second call has to arrive while two activities share an activityId, its dedupe has to keep a different activity from the one the first call's loop keeps, and both have to end an activity at the same moment.

Testing

Unit testing

Tests with fake activities cover grouping by activityId (ended and dismissed activities excluded), choosing the activity to keep, ending all but one per group, and finding a live duplicate on dismissal. They also cover picking a new keeper when the keeper ends during the dedupe, and calling exit when no activity stays live. Two executor tests cover reading the cached update token, including when a removal is pending. ActivityKit activities cannot be created in unit tests, so the wiring in listenForActivity and handleDismissal is not covered.

Manual testing

On an iPhone 17 simulator (iOS 26.5), the full Reduced unit test plan passed 650 of 651 tests. The one failure is UserJwtLifecycleTests.testUpdateUserJwtSendsTheCreateUserThatARejectedTokenHeld in OneSignalUserTests, which does not depend on Live Activities. It passed 3 of 3 runs alone.

On an iPhone running iOS 26 with two subscriptions sharing one push-to-start token (a second user and push subscription created after clearing the SDK cache), the app was swiped away and one push-to-start was sent. It reached both subscriptions, and one widget remained once the app launched and setup() ran.

On an iPhone 17 Pro simulator (iOS 26.5), a demo build from main was compared with a build from main with this branch merged in. On 19 November one push-to-start reached the device once, even with two subscriptions sharing its token, so these runs sent the start twice with the same activityId.

  • With the app not running, both pushes started an activity and iOS launched the app in the background. The main build kept both widgets. The build with this branch ended one in setup() and kept one.
  • With two activities sharing an activityId, one update was sent. The main build registered both update tokens under the activityId, and the update reached one activity while the other kept its old content. The build with this branch kept one activity, and the update reached it. These two activities were started in the app before setup() ran, because the simulator gives no update token to an activity started by push.

Dismissing one widget while a duplicate is live was not tested.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

🤖 Generated with Claude Code

Activities with the same OneSignal activityId share one update token registration,
so dismissing a duplicate froze the survivor and only the last registered widget updated.
@nan-li
nan-li marked this pull request as ready for review October 29, 2026 16:15

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-model review

Reviewed with Claude Opus 5.5, GPT 5.6 Sol, and Grok 4.7. Substantive diff is 311 lines after excluding project.pbxproj.

Act on (3/3): Setup now ends duplicates in an unstructured Task, while the existing activityUpdates loop still ends every other live instance and listens to whoever the stream yields. Those policies disagree (cached token vs newest), and Apple already notes the same activity can appear in both places on push-to-start. If they interleave, both widgets can be ended, or listeners attach to the copy setup is about to kill.

Act on (3/3): endDuplicates chooses a keeper once, then still ends remaining live copies if that keeper dies mid-await. testEndDuplicates_whenTheKeeperEndsDuringTheAwaits_returnsNothing locks that in. The caller then attaches no listeners and never exits, so the backend can keep a dead token.

Consider: Skipping exit on dismiss does not enter the survivor's token, so the last registration can still belong to the dismissed activity. Ended-but-still-listed activities are omitted from duplicateGroups and can still enter. cachedUpdateToken treats a pending/unsuccessful OSRequestSetUpdateToken as the backend token. activityToKeep can pick the wrong instance while pushToken is still nil.

Noted: Keeper listeners wait until all ends finish, so contentUpdates in that window are missed. Activity.activities order is unspecified for the no-token fallback.

Open in Web View Automation 

Sent by Cursor Automation: PR Reviews

Setup kept the activity with the cached token and the activityUpdates loop kept the newest, so both at once could end every duplicate.
A dismissal with a live duplicate now registers that duplicate's token, because the dismissed activity may hold the registration.

Co-authored-by: Cursor <cursoragent@cursor.com>
@nan-li

nan-li commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Replies to the other points in the Cursor review summary:

  • The dismissal path now registers the survivor's token, fixed in c64c904. When a live duplicate exists, the SDK calls enter with that duplicate's token if enablePushToUpdate is on. The executor drops the request when the token already matches.
  • I did not change how ended activities call enter. The code before this PR also listened to every activity in Activity.activities, so this is not a regression, and I kept it out of scope.
  • cachedUpdateToken returns a pending request on purpose. pollPendingRequests resends unsuccessful requests, so the cache holds the token the SDK will send. Keeping the activity that matches it is correct.
  • activityToKeep with nil tokens works as intended. If no activity has a token, the backend has none either, and the keeper registers its token through pushTokenUpdates. Any pick works, so the order of Activity.activities does not matter.
  • A content update that arrives before the keeper gets listeners can lose its receive receipt, and I accept that. The token is not lost, because listenForActivityPushToUpdate reads activity.pushToken first.

@nan-li
nan-li requested a review from a team November 13, 2026 17:47

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two earlier comments are fixed. The activityUpdates loop waits for this call's setup dedupe and skips a yield that is no longer live, and endDuplicates picks a new keeper if the current one ends during await end.

Dismissal still leaves the update token in place when the survivor has no token yet, and a second setup() can start a second loop.

@fadi-george

Copy link
Copy Markdown
Contributor

Same as ar's assessment.

When an activity is dismissed and several live activities share its
activityId, the SDK registers the token of one that has a token. It
used to take the first live one, which could have no token yet.
@nan-li
nan-li requested review from a team and abdulraqeeb33 November 20, 2026 17:50

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting, not requesting changes.

The description covers one setup() call. The activityUpdates loop waits for that call's dedupe, so those two rules do not run at once. A second setup() for the same activity type is only in the thread reply. On main both paths keep the activity just yielded, so a second call lands on the same one. Here the second dedupe keeps the cached-token activity while the first loop keeps the new one. If those ends overlap, nothing stays live and endDuplicates calls exit.

On the skipped exit: an exit built while the survivor's token reads nil is older than a later enter, and the enter replaces it in the cache. The case that stays is when no enter follows, enablePushToUpdate off or a survivor that never receives a push token. The dismissed token stays registered. That is not in the description. The description says the new dismissal behavior runs only when enablePushToUpdate is on, and that nothing changes for apps that call enter and exit themselves.

@nan-li
nan-li requested review from a team and abdulraqeeb33 November 23, 2026 16:35
@nan-li

nan-li commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Replies to the two points in the latest review:

  • On a second setup() call, I am not adding a guard. For every copy to end, an app has to call setup() twice for the same activity type, the second call has to arrive in the instant two activities share an activityId, the two paths have to keep different activities, and both have to end one at the same moment. I do not expect that to happen in production. The description now lists it as a case left as it is. You are right that main's loops agree and these two can disagree.
  • On the skipped exit, I am leaving the code as it is. A token only stays registered when a dismissal happens while a duplicate is live and no enter follows, which means enablePushToUpdate is off or the remaining activity never receives a token. Even then the remaining widget is not affected, and exit runs when it is dismissed. That is too unlikely, and too harmless when it does happen, to justify another branch here. The description now states it. You are right that a later enter replaces the exit, so the ordering reason I gave earlier does not hold.

@fadi-george fadi-george left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the remaining cases are documented and low impact.

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The earlier blocking items are fixed and the two residuals in the description are fine.

  1. handleDismissal is the only new helper without a seam, so exit / enter / leave-registered are untested, and dismissal-while-duplicate was not manually tested. A pure dismissalAction over FakeActivity would close that.
  2. The "keeping duplicate" log is the same when the token is re-entered and when it is left registered. Worth a debug line on the leave path.
  3. The exit comment at endDuplicates overclaims. That call covers groups that never got a state listener; a later setup() can still hit handleDismissal.

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 005a8dcb. Prior blockers are fixed: setup awaits its dedupe, keeper re-pick works, and liveDuplicate prefers a tokened survivor. The skipped exit when no survivor has a token, and the second-setup() residual, still look right as documented.

Two nits only.

@nan-li
nan-li merged commit 07c2f36 into main Dec 1, 2026
6 checks passed
@nan-li
nan-li deleted the nan/sdk-5580 branch December 1, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants