Skip to content

fix(push): capture $push_notification_opened on iOS cold start - #556

Draft
turnipdabeets wants to merge 3 commits into
mainfrom
fix/push-open-cold-start
Draft

fix(push): capture $push_notification_opened on iOS cold start#556
turnipdabeets wants to merge 3 commits into
mainfrom
fix/push-open-cold-start

Conversation

@turnipdabeets

@turnipdabeets turnipdabeets commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Fixes #555$push_notification_opened is never captured on iOS. Reproduced on a simulator; two independent causes:

  1. Nobody is listening. A stock Flutter app sets no UNUserNotificationCenter delegate, so iOS reports the tap to nobody and the native SDK's swizzling has nothing to attach to. (Note flutter_local_notifications does not set it either — it registers as an application delegate and relies on FlutterAppDelegate forwarding.)
  2. We start too late. On a cold launch from a tap, didReceive arrives ~150 ms in, about 90 ms before Dart reaches Posthog().setup().

⚠️ Blocked on PostHog/posthog-ios#792 — this cannot compile until that ships PostHogSDK.prewarmPushNotificationOpenCapture(). The podspec and Package.swift floors are already raised to >= 3.72.0, so merging or releasing this before posthog-ios 3.72.0 is published fails pod install / SPM resolution for every iOS user — a hard build break, not a degradation. Draft until then.

💚 How did you test it?

On device — posthog-flutter example app, iPhone 17 Pro sim / iOS 26.4, built against a local posthog-ios with the native change:

  • 4/4 notification taps captured, one event each (1 warm start, 3 cold starts), confirmed in ingestion. Before the fix, cold starts captured 0 and warm starts captured 0 for want of a delegate.
  • Warm start captures exactly once — no double-counting from the new buffered path.
  • With the delegate removed, the new native warning fires; with it set, it does not.

flutter analyze clean. The native unit tests live in the posthog-ios PR.

Testing

Verified on an iPhone 17 Pro simulator (iOS 26.4) against a local posthog-ios build, with events confirmed in a real PostHog project:

Scenario Result
Cold launch from a tap captured (was 0 before this change)
Tap while the app is running (warm) captured
Launch with no push payload not captured
capturePushNotificationOpened: false not captured, and the integration never installs
Example AppDelegate with the delegate line removed the new debug warning fires
Delegate present no warning

Repeated across four rounds — 15 $push_notification_opened events ingested in total, carrying $notification_title and $notification_body.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Built with Claude Code (session), driven by @turnipdabeets. Nearly all the logic lives in posthog-ios; this side is the trigger plus the example wiring.

Worth a reviewer's attention:

  • prewarmPushNotificationOpenCapture() is called from register(with:) because that runs inside didFinishLaunchingWithOptions. AUTO_INIT would also win the race (verified), but it is mutually exclusive with pushIdentityProvider, so it isn't a usable answer for everyone.
  • The Info.plist key is the only opt-out reachable that early — a Dart-side capturePushNotificationOpened: false isn't known yet. The native SDK releases an unwanted prewarm at setup(), so this only matters for one add-to-app ordering, documented at the call site. That widens the key's scope beyond AUTO_INIT apps, hence the second changeset.
  • The example AppDelegate change is the fix for cause (1) and is the copy-pasteable bit for users hitting this.

Not done here, deliberately: a README/docs section (the SDK currently ships push with no iOS setup docs at all). The identical cold-start race exists in @posthog/react-native-plugin and needs a follow-up issue on posthog-js.

Plugin registration runs inside didFinishLaunchingWithOptions, early
enough to prewarm the native push-open swizzles before the tap response
is delivered — Dart-side setup() lands about 90ms too late.

Also wires UNUserNotificationCenter.current().delegate into the example
app; without a delegate iOS reports the tap to nobody and no open is
capturable at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012txiHBCZRkShMdE7V25Jrd
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

posthog-flutter Compliance Report

Date: 2026-09-02 15:50:53 UTC
Duration: 96644ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 132ms
Format Validation.Event Has Uuid 117ms
Format Validation.Event Has Lib Properties 115ms
Format Validation.Distinct Id Is String 114ms
Format Validation.Token Is Present 114ms
Format Validation.Custom Properties Preserved 115ms
Format Validation.Event Has Timestamp 115ms
Retry Behavior.Retries On 503 5325ms
Retry Behavior.Does Not Retry On 400 2116ms
Retry Behavior.Does Not Retry On 401 2117ms
Retry Behavior.Respects Retry After Header 8124ms
Retry Behavior.Implements Backoff 15444ms
Retry Behavior.Retries On 500 5225ms
Retry Behavior.Retries On 502 5225ms
Retry Behavior.Retries On 504 5224ms
Retry Behavior.Max Retries Respected 15437ms
Deduplication.Generates Unique Uuids 121ms
Deduplication.Preserves Uuid On Retry 5223ms
Deduplication.Preserves Uuid And Timestamp On Retry 10334ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5228ms
Deduplication.No Duplicate Events In Batch 121ms
Deduplication.Different Events Have Different Uuids 114ms
Compression.Sends Gzip When Enabled 113ms
Batch Format.Uses Proper Batch Structure 113ms
Batch Format.Flush With No Events Sends Nothing 108ms
Batch Format.Multiple Events Batched Together 121ms
Error Handling.Does Not Retry On 403 2114ms
Error Handling.Does Not Retry On 413 2116ms
Error Handling.Retries On 408 5222ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 12ms
Request Payload.Flags Request Uses V2 Query Param 8ms
Request Payload.Flags Request Hits Flags Path Not Decide 8ms
Request Payload.Flags Request Omits Authorization Header 8ms
Request Payload.Token In Flags Body Matches Init 7ms
Request Payload.Groups Round Trip 8ms
Request Payload.Groups Default To Empty Object 8ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 8ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 7ms
Request Payload.Disable Geoip Omitted Defaults To False 7ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 8ms
Request Lifecycle.No Flags Request On Init Alone 3ms
Request Lifecycle.No Flags Request On Normal Capture 110ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 14ms
Request Lifecycle.Mock Response Value Is Returned To Caller 8ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 113ms

…quisite

The podspec and Package.swift still allowed 3.70.0/3.71.0, neither of which
has prewarmPushNotificationOpenCapture(), so a consumer with a locked
Podfile.lock would have hit a compile error rather than the fix.

Also names the UNUserNotificationCenter delegate requirement, which is the
other half of the reported bug and was previously only fixed in the example.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012txiHBCZRkShMdE7V25Jrd
It still named 3.70.0 directly above the raised 3.72.0 requirement.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012txiHBCZRkShMdE7V25Jrd
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.

iOS: $push_notification_opened not captured (warm or cold start) when flutter_local_notifications owns the notification delegate

1 participant