> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # iOS push notifications Canonical URL: https://posthog.com/docs/workflows/push-notifications/ios ## On this page - [Requirements](#requirements) - [Automatic registration and open tracking (default)](#automatic-registration-and-open-tracking-default) - [Manual registration](#manual-registration) - [Capturing opens](#capturing-opens) - [Identity verification](#identity-verification) - [Troubleshooting](#troubleshooting) Set up [Workflows](https://posthog.com/docs/workflows.md) push notifications in the iOS SDK. For the concept and channel setup, see [Push notifications](https://posthog.com/docs/workflows/push-notifications.md). Available in the iOS SDK version 3.69.0 and newer. ## Requirements - Enable the **Push Notifications** capability for your app and register for remote notifications (`registerForRemoteNotifications()`), which requires requesting notification permission from the user. - A connected **FCM** or **APNs** channel in PostHog whose Firebase project / APNs topic (bundle id) matches your app. - **iOS 14 or newer** for the automatic hooks. On iOS 13 the SDK installs neither the registration nor the open-capture hook, so no push events are captured. Everything else the SDK captures is unaffected. ## Automatic registration and open tracking (default) By default the SDK registers the device token and captures opens for you, by hooking into the system callbacks: ```swift let config = PostHogConfig(apiKey: "") // Both default to true, shown here for clarity: config.capturePushNotificationSubscriptions = true // register the device token with PostHog config.capturePushNotificationOpened = true // capture `$push_notification_opened` on tap PostHogSDK.shared.setup(config) ``` With these enabled you only need to register for remote notifications; the SDK picks up the token and open events automatically. The token is registered under the current distinct ID, so it follows the user across `identify()`. Locally-scheduled notifications are ignored. Capture those manually (below). **Your app must set a notification delegate** iOS reports a notification tap through `UNUserNotificationCenter.current().delegate`. If your app never sets one, the SDK has nothing to observe and `$push_notification_opened` is never captured, whatever the app state. Set the delegate when your app launches: ```swift import UserNotifications class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { UNUserNotificationCenter.current().delegate = self return true } } ``` The SDK logs a warning a few seconds after `setup()` when the delegate is still missing, if you enable `config.debug`. Automatic registration and open capture require `config.enableSwizzling` to be `true` (the default). If you disable swizzling or manage your own delegates, use the manual APIs below. ## Manual registration If you manage the notification lifecycle yourself, turn the automatic flags off and call the SDK directly. Register the device token from `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)`: ```swift func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let token = deviceToken.map { String(format: "%02x", $0) }.joined() PostHogSDK.shared.registerPushNotificationToken(token) // Or, to route to a specific channel by app id (Firebase project id / APNs bundle id): // PostHogSDK.shared.registerPushNotificationToken(token, appId: "com.example.app") } ``` Unregister the token when a user signs out so it isn't left bound to them: ```swift PostHogSDK.shared.unregisterPushNotificationToken() ``` Calling `PostHogSDK.shared.reset()` on logout also unregisters the token for the signed-out user and re-registers it under the new anonymous id. Registration and unregistration are durable. If the device is offline or the request fails, the SDK retries on the next `flush()`, identity change, or app launch. ## Capturing opens When automatic capture is on, tapping a remote push emits a `$push_notification_opened` event. To capture opens yourself (or to capture a locally-scheduled notification), call: ```swift func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { PostHogSDK.shared.capturePushNotificationOpened(response: response) completionHandler() } ``` There's also a fully manual variant if you don't have a `UNNotificationResponse`: ```swift PostHogSDK.shared.capturePushNotificationOpened(title: "...", body: "...", payload: [:], action: "...") ``` From iOS SDK 3.75.0, both calls are deduplicated against automatic capture: a repeat of a notification PostHog sent, captured in the last 5 minutes, is skipped, so one tap counts once whichever path reports it. Notifications PostHog didn't send are never deduplicated. The field-based call carries no notification identifier, so a genuine re-send of the same workflow step inside those 5 minutes reads as the same tap and is skipped – report a re-send through `capturePushNotificationOpened(response:)`, which can tell the deliveries apart. The `$push_notification_opened` event includes `$notification_title`, `$notification_subtitle`, and `$notification_body`, plus `$notification_action` for action-button taps. On iOS, notification content is only captured for notifications sent by PostHog. Opens of other notifications are still captured, but without title, subtitle, or body. ## Identity verification If your push channel requires [identity verification](https://posthog.com/docs/workflows/push-notifications.md#identity-verification), supply a backend-minted token through `pushIdentityProvider`: ```swift config.pushIdentityProvider = { distinctId, appId, completion in // Fetch a freshly-minted token from your backend for this user, then: completion(token) // or completion(nil) to send without one } ``` ## Troubleshooting | Issue | Check | | --- | --- | | Token never registers | Confirm you call `registerForRemoteNotifications()` and the user granted notification permission. If you set `config.enableSwizzling = false`, automatic registration and open capture are off. Use the manual APIs. | | Push doesn't arrive | Confirm the channel's APNs environment (Production/Sandbox) matches your build, and the bundle id matches. | | Opens are never captured | Confirm your app sets `UNUserNotificationCenter.current().delegate`, that `config.enableSwizzling` is `true`, and that the device runs iOS 14 or newer. In a cross-platform host that configures PostHog from Dart or JavaScript, call `PostHogSDK.prewarmPushNotificationOpenCapture()` (iOS SDK 3.72.0 and newer) from `application(_:didFinishLaunchingWithOptions:)` so a cold-start tap isn't lost before `setup()` runs. The PostHog Flutter and React Native plugins already do this for you. | | Registration rejected on a Required channel | Your `pushIdentityProvider` isn't returning a valid token in time. See [Identity verification](https://posthog.com/docs/workflows/push-notifications.md#identity-verification). |