Skip to content

fix(push): re-register the device token on opt-in (#746) - #760

Merged
dmarchuk merged 3 commits into
mainfrom
fix/push-optin-reregister-746
Aug 11, 2026
Merged

dmarchuk merged 3 commits into
mainfrom
fix/push-optin-reregister-746

Conversation

@dmarchuk

Copy link
Copy Markdown
Contributor

Problem

The opt-in half of #746. After a logout unregister clears the stored push token, optIn() only restores consent and the notification observer. It never refetches the APNs token or re-registers the device, so a user who opts out and back in stays unsubscribed and Workflows can't deliver to them until the next app restart re-runs registration.

Change

On optIn(), when capturePushNotificationSubscriptions is enabled, the SDK now re-requests the APNs device token. The OS redelivers it to the (re-installed) subscription integration, which re-registers the device. A new PostHogPushNotificationSubscriptionIntegration.requestTokenRefresh hook encapsulates the registerForRemoteNotifications() call (with the same app-context guard start() uses) and is overridable in tests.

Manual push mode (capturePushNotificationSubscriptions = false) is unchanged: the host owns the token lifecycle, so opt-in does not auto-refetch.

How tested

  • Unit tests in PostHogPushNotificationTest: opt-in re-requests the token when auto-capture is on, and does not when it's off.
  • Real-device E2E (iPhone, real backend): register, opt out (which clears the token), then opt in, and observed a re-register POST /api/push_subscriptions. With the refetch disabled, the same taps produced no re-register, confirming the fix is what re-arms push.

Not in this PR

The unregister-DELETE-during-opt-out half of #746 is fixed separately in #759. This PR is independent and targets main.

🤖 Draft — opening for review; will un-draft after self-review.

@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

posthog-ios Compliance Report

Date: 2026-08-11 10:41:28 UTC
Duration: 210791ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

✅ 29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields ✅ 2841ms
Format Validation.Event Has Uuid ✅ 313ms
Format Validation.Event Has Lib Properties ✅ 303ms
Format Validation.Distinct Id Is String ✅ 2768ms
Format Validation.Token Is Present ✅ 2721ms
Format Validation.Custom Properties Preserved ✅ 2738ms
Format Validation.Event Has Timestamp ✅ 412ms
Retry Behavior.Retries On 503 ✅ 11183ms
Retry Behavior.Does Not Retry On 400 ✅ 4796ms
Retry Behavior.Does Not Retry On 401 ✅ 4788ms
Retry Behavior.Respects Retry After Header ✅ 5395ms
Retry Behavior.Implements Backoff ✅ 21261ms
Retry Behavior.Retries On 500 ✅ 9294ms
Retry Behavior.Retries On 502 ✅ 9188ms
Retry Behavior.Retries On 504 ✅ 5307ms
Retry Behavior.Max Retries Respected ✅ 15423ms
Deduplication.Generates Unique Uuids ✅ 2952ms
Deduplication.Preserves Uuid On Retry ✅ 9307ms
Deduplication.Preserves Uuid And Timestamp On Retry ✅ 16169ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 7831ms
Deduplication.No Duplicate Events In Batch ✅ 3027ms
Deduplication.Different Events Have Different Uuids ✅ 2800ms
Compression.Sends Gzip When Enabled ✅ 2765ms
Batch Format.Uses Proper Batch Structure ✅ 2790ms
Batch Format.Flush With No Events Sends Nothing ✅ 295ms
Batch Format.Multiple Events Batched Together ✅ 2915ms
Error Handling.Does Not Retry On 403 ✅ 4802ms
Error Handling.Does Not Retry On 413 ✅ 2348ms
Error Handling.Retries On 408 ✅ 8730ms

Feature_Flags Tests

✅ 16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 2679ms
Request Payload.Flags Request Uses V2 Query Param ✅ 2829ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 2812ms
Request Payload.Flags Request Omits Authorization Header ✅ 2669ms
Request Payload.Token In Flags Body Matches Init ✅ 2729ms
Request Payload.Groups Round Trip ✅ 2769ms
Request Payload.Groups Default To Empty Object ✅ 2765ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It ✅ 2801ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 2803ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 2777ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 2821ms
Request Lifecycle.No Flags Request On Init Alone ✅ 75ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 2687ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 5474ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 2790ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 2872ms

@dmarchuk
dmarchuk marked this pull request as ready for review August 10, 2026 16:25
@dmarchuk
dmarchuk requested a review from a team as a code owner August 10, 2026 16:25
@greptile-apps

greptile-apps Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(push): re-register the device token ..." | Re-trigger Greptile

Comment thread PostHog/PostHogSDK.swift Resolved
@marandaneto
marandaneto requested a review from a team August 10, 2026 18:28

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

approving to unblock but see comments

@dmarchuk
dmarchuk merged commit a164d38 into main Aug 11, 2026
53 of 54 checks passed
@dmarchuk
dmarchuk deleted the fix/push-optin-reregister-746 branch August 11, 2026 12:52
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.

2 participants