Skip to content

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

Open
turnipdabeets wants to merge 4 commits into
mainfrom
fix/push-open-cold-start
Open

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

Conversation

@turnipdabeets

@turnipdabeets turnipdabeets commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🔗 Related PRs

One fix, five PRs — three SDKs plus the docs. Each Flutter PR is gated on the native release it depends on.

PR What it does
PostHog/posthog-ios#792 native iOS — hold a tap delivered before setup()
PostHog/posthog-android#753 native Android — read a launch intent the SDK installed too late to see
#556 Flutter iOS cold start · posthog-ios#792 merged, awaiting the 3.72.0 release · fixes #555 — ← this PR
#557 Flutter Android cold + warm start · needs posthog-android 3.62.0 · fixes #558
PostHog/posthog.com#19905 docs for all of the above

Order: PostHog/posthog-ios#792 and PostHog/posthog-android#753 merge and release first → #556 and #557 leave draft and go green on their own once the floors publish → PostHog/posthog.com#19905 last, since posthog.com deploys on merge.

💡 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().

Waiting on the posthog-ios release, not on review. PostHog/posthog-ios#792 merged (8566372). main is at 3.71.6 with two pending changesets — minor for the prewarm API and patch for the missing-delegate warning — so the next release is 3.72.0, which is exactly the floor declared here (>= 3.72.0 in the podspec, 3.72.0 ..< 4.0.0 in Package.swift).

Until 3.72.0 is on CocoaPods trunk and tagged for SPM, merging or releasing this fails pod install / SPM resolution for every iOS user — a hard build break, not a degradation. Draft until the release lands, then this goes green without a code change.

💚 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), each read out of the /batch payload the SDK sent. 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 each event read out of the SDK's own /batch payload:

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 in total, each carrying $notification_title and $notification_body.

Re-verified 2026-09-08 on an iPhone 17 simulator (iOS 26.4), this branch built against posthog-ios#792:

  • Cold-launch tap captured — and the event leaves in the first batch, at the same millisecond as the config request, which is the buffered-then-replayed path this PR exists for.
  • Warm tap captured exactly once.
  • Plain launch with no notification: nothing captured.
  • capturePushNotificationOpened: false: nothing captured — and flipping it back on re-fires the same notification, so the negative is the flag and not a stale build.

⚠️ CI has not compiled this branch's Swift yet. All four build-apple jobs and Analyze (swift) die at dependency resolution on the not-yet-published PostHog >= 3.72.0 floor, so the red checks say nothing about the code itself. It builds clean locally against the merged posthog-ios branch, and the red turns green on its own once 3.72.0 publishes — the same way #557 went from red to 27/27 the moment posthog-android 3.62.0 hit Maven Central.

📝 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.

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

posthog-flutter Compliance Report

Date: 2026-09-08 01:53:56 UTC
Duration: 96727ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 136ms
Format Validation.Event Has Uuid 120ms
Format Validation.Event Has Lib Properties 115ms
Format Validation.Distinct Id Is String 114ms
Format Validation.Token Is Present 113ms
Format Validation.Custom Properties Preserved 116ms
Format Validation.Event Has Timestamp 115ms
Retry Behavior.Retries On 503 5326ms
Retry Behavior.Does Not Retry On 400 2116ms
Retry Behavior.Does Not Retry On 401 2116ms
Retry Behavior.Respects Retry After Header 8124ms
Retry Behavior.Implements Backoff 15445ms
Retry Behavior.Retries On 500 5224ms
Retry Behavior.Retries On 502 5224ms
Retry Behavior.Retries On 504 5224ms
Retry Behavior.Max Retries Respected 15439ms
Deduplication.Generates Unique Uuids 123ms
Deduplication.Preserves Uuid On Retry 5223ms
Deduplication.Preserves Uuid And Timestamp On Retry 10334ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5229ms
Deduplication.No Duplicate Events In Batch 121ms
Deduplication.Different Events Have Different Uuids 115ms
Compression.Sends Gzip When Enabled 116ms
Batch Format.Uses Proper Batch Structure 112ms
Batch Format.Flush With No Events Sends Nothing 107ms
Batch Format.Multiple Events Batched Together 124ms
Error Handling.Does Not Retry On 403 2115ms
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 9ms
Request Payload.Token In Flags Body Matches Init 8ms
Request Payload.Groups Round Trip 9ms
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 8ms
Request Payload.Disable Geoip Omitted Defaults To False 8ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 8ms
Request Lifecycle.No Flags Request On Init Alone 4ms
Request Lifecycle.No Flags Request On Normal Capture 110ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 13ms
Request Lifecycle.Mock Response Value Is Returned To Caller 8ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 114ms

turnipdabeets and others added 3 commits September 7, 2026 21:29
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
…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
@turnipdabeets
turnipdabeets force-pushed the fix/push-open-cold-start branch from 88487df to f8e8246 Compare September 8, 2026 01:30
The Dart flag can't reach the prewarm — it runs at plugin registration,
before Dart does. Only the plist key opts it out, and that lived solely
in a changeset.

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

greptile-apps Bot commented Sep 9, 2026

Copy link
Copy Markdown
Prompt To Fix All With AI
### Issue 1
posthog_flutter/darwin/posthog_flutter/Sources/posthog_flutter/PosthogFlutterPlugin.swift:108
**Prewarm skips supported iOS 13**

The package still supports iOS 13, but this availability check skips the prewarm there. When a notification tap cold-launches an iOS 13 app, the response can still arrive before Dart setup without being buffered, so the advertised cold-start fix remains ineffective on a supported deployment target.

### Issue 2
posthog_flutter/darwin/posthog_flutter/Sources/posthog_flutter/PosthogFlutterPlugin.swift:86
**Prewarm path lacks coverage**

The new registration-time prewarm and its plist opt-out have no automated regression coverage. Please add a native test seam that verifies registration invokes prewarm when the key is enabled or absent and suppresses it when false; otherwise, removing the early call or reversing the guard could silently reintroduce the cold-start failure.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "docs(push): name the Info.plist opt-out ..." | Re-trigger Greptile

private static func prewarmPushNotificationOpenCapture() {
let capturePushNotificationOpened = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.CAPTURE_PUSH_NOTIFICATION_OPENED") as? Bool ?? true
guard capturePushNotificationOpened else { return }
if #available(iOS 14.0, macOS 11.0, *) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Prewarm skips supported iOS 13

The package still supports iOS 13, but this availability check skips the prewarm there. When a notification tap cold-launches an iOS 13 app, the response can still arrive before Dart setup without being buffered, so the advertised cold-start fix remains ineffective on a supported deployment target.

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog_flutter/darwin/posthog_flutter/Sources/posthog_flutter/PosthogFlutterPlugin.swift
Line: 108

Comment:
**Prewarm skips supported iOS 13**

The package still supports iOS 13, but this availability check skips the prewarm there. When a notification tap cold-launches an iOS 13 app, the response can still arrive before Dart setup without being buffered, so the advertised cold-start fix remains ineffective on a supported deployment target.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

let instance = PosthogFlutterPlugin()
instance.channel = methodChannel
PosthogFlutterPlugin.instance = instance
prewarmPushNotificationOpenCapture()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Prewarm path lacks coverage

The new registration-time prewarm and its plist opt-out have no automated regression coverage. Please add a native test seam that verifies registration invokes prewarm when the key is enabled or absent and suppresses it when false; otherwise, removing the early call or reversing the guard could silently reintroduce the cold-start failure.

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog_flutter/darwin/posthog_flutter/Sources/posthog_flutter/PosthogFlutterPlugin.swift
Line: 86

Comment:
**Prewarm path lacks coverage**

The new registration-time prewarm and its plist opt-out have no automated regression coverage. Please add a native test seam that verifies registration invokes prewarm when the key is enabled or absent and suppresses it when false; otherwise, removing the early call or reversing the guard could silently reintroduce the cold-start failure.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant