> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Android push notifications Canonical URL: https://posthog.com/docs/workflows/push-notifications/android ## 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 Android SDK. For the concept and channel setup, see [Push notifications](https://posthog.com/docs/workflows/push-notifications.md). Available in the Android SDK version 3.58.0 and newer. Sends go out through the FCM channel you connect in [Workflows > Channels](https://posthog.com/docs/workflows/configure-channels?tab=Push.md), so the Firebase project on that channel must match your app. ## Requirements Set up Firebase Cloud Messaging and request the `POST_NOTIFICATIONS` runtime permission from the user (required on Android 13+) so the system can show notifications. ## Automatic registration and open tracking (default) Both behaviors are on by default. When `firebase-messaging` is on your classpath, the SDK registers this device's FCM token with PostHog, and it auto-captures a `$push_notification_opened` event when a user opens the app from a notification tray tap. ```kotlin val config = PostHogAndroidConfig(apiKey = "").apply { capturePushNotificationSubscriptions = true // register the FCM token with PostHog capturePushNotificationOpened = true // capture `$push_notification_opened` } PostHogAndroid.setup(context, config) ``` **Initialize the SDK in your `Application.onCreate`, not an Activity.** The SDK installs its open-tracking hook during `setup()`, so it has to run before the launching Activity is created. If you initialize inside an Activity, a cold-start tap on a notification won't be captured. Firebase delivers rotated tokens through `onNewToken`, which the SDK can't observe on its own, so forward it to keep the registered token current: ```kotlin class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { PostHog.registerPushNotificationToken(token, appId = "your-firebase-project-id") } } ``` ## Manual registration ```kotlin // Register a device token (appId = your Firebase project id): PostHog.registerPushNotificationToken(token, appId = "your-firebase-project-id") // Unregister when a user signs out: PostHog.unregisterPushNotificationToken() ``` Calling `PostHog.reset()` on logout 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. ### Firebase Installation IDs Firebase is moving from FCM registration tokens to Firebase Installation IDs. If your app turns on installation ID mode (the `firebase_messaging_installation_id_enabled` manifest flag), `getToken()` throws. From Android SDK 3.75.0, the SDK registers the installation ID instead when your app starts. Also forward `onRegistered` (firebase-messaging 25.1.0 and later), so the device is registered again when Firebase calls it: ```kotlin class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onRegistered(installationId: String) { PostHog.registerPushNotificationToken(installationId, appId = "your-firebase-project-id") } } ``` Firebase says FCM accepts an installation ID in place of a registration token during its transition period, so pushes to a registered installation ID should be delivered. Firebase hasn't announced when that period ends. ## Capturing opens Automatic open capture detects cold-start taps on a notification from the system tray, recognized by the `google.message_id` extra that Firebase puts on the intent. Android gives libraries no way to observe `Activity.onNewIntent`, so a tap that arrives while your app is already running needs one line in your activity: ```kotlin override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) PostHogAndroid.capturePushNotificationOpened(intent) } ``` Your launcher activity needs `android:launchMode="singleTop"`, or the system resumes the task instead of delivering the tap here. Requires Android SDK 3.62.0 or newer. `PostHogAndroid.capturePushNotificationOpened` is deduplicated against the automatic path by message ID, so it can't double-count. From Android SDK 3.65.0, `PostHog.capturePushNotificationOpened` below is deduplicated too: a repeat of a notification PostHog sent, captured in the last 5 minutes, is skipped, so one tap counts once whichever path reports it. That second dedupe only covers notifications PostHog sent; the message-ID dedupe above applies to any FCM notification. Notifications you display yourself from a foreground data message, and push delivered outside FCM, aren't detected at all. Capture those with the fully manual API: ```kotlin PostHog.capturePushNotificationOpened( title = title, body = body, payload = message.data, ) ``` The `$push_notification_opened` event includes `$notification_title` and `$notification_body`, plus `$notification_action` for action-button taps. Android taps captured automatically carry no title or body – Firebase strips the notification content from the intent it hands the app, so there is nothing to read. Title and body are only set when you pass them to `PostHog.capturePushNotificationOpened` yourself. ## 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`: ```kotlin config.pushIdentityProvider = { distinctId, appId, completion -> // Fetch a freshly-minted token from your backend for this user, then: completion(token) // or completion(null) to send without one } ``` ## Troubleshooting | Issue | Check | | --- | --- | | Token never registers | Confirm `firebase-messaging` is on the classpath and your Firebase setup (`google-services.json`) is in place, and that you forward rotated tokens from `onNewToken`. | | Push doesn't arrive | Confirm the Firebase project on your Workflows channel matches your app, and the user granted the `POST_NOTIFICATIONS` permission. | | 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). |