> 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/android ## On this page - [Requirements](#requirements) - [Register the device](#register-the-device) - [Capture opens](#capture-opens) - [Identity verification](#identity-verification) - [Troubleshooting](#troubleshooting) Register Android devices for [push notifications](https://posthog.com/docs/workflows/push.md) with the PostHog Android SDK, and capture when users open them. Before you start, [connect an FCM channel](https://posthog.com/docs/workflows/push.md#connect-a-push-channel) for the same Firebase project as your app. ## Requirements - Android SDK 3.58.0 or later. Versions before 3.72.0 take `apiKey` instead of `projectToken`. - Firebase Cloud Messaging set up in your app, with `firebase-messaging` on the classpath. - The `POST_NOTIFICATIONS` permission, which your app asks for on Android 13 and later. ## Register the device The SDK registers the device's FCM token and captures opens by default: ```kotlin val config = PostHogAndroidConfig(projectToken = "").apply { // Both default to true capturePushNotificationSubscriptions = true // register the FCM token capturePushNotificationOpened = true // capture $push_notification_opened } PostHogAndroid.setup(context, config) ``` Call `setup()` in `Application.onCreate`, not in an Activity. The SDK reads a notification tap when the launch Activity is created, so it has to be set up before that. If you set it up in an Activity, it misses taps that launch the app. The SDK fetches the token at startup. Firebase sends a new token to `onNewToken`, which the SDK can't see, so forward it: ```kotlin class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { PostHog.registerPushNotificationToken(token, appId = "your-firebase-project-id") } } ``` ### Register manually If you manage tokens yourself, set `capturePushNotificationSubscriptions` to `false` and register the token with your Firebase project ID as the app ID: ```kotlin PostHog.registerPushNotificationToken(token, appId = "your-firebase-project-id") ``` If you don't call `reset()` at sign-out, unregister the device yourself: ```kotlin PostHog.unregisterPushNotificationToken() ``` ### Firebase installation IDs If your app turns on the `firebase_messaging_installation_id_enabled` manifest flag, the SDK registers the Firebase installation ID at startup instead of the FCM token (Android SDK 3.75.0 and later). Forward `onRegistered` instead of `onNewToken`, so the device registers again when Firebase calls it: ```kotlin class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onRegistered(installationId: String) { PostHog.registerPushNotificationToken(installationId, appId = "your-firebase-project-id") } } ``` ## Capture opens The SDK captures a tap that launches your app from the system tray. It recognizes the tap by the `google.message_id` extra that Firebase puts on the intent. A tap that arrives while your app is running goes to `Activity.onNewIntent`, which the SDK can't see. Forward it from your launcher activity (Android SDK 3.62.0 and later): ```kotlin override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) PostHogAndroid.capturePushNotificationOpened(intent) } ``` Your launcher activity needs `android:launchMode="singleTop"`. Otherwise the system resumes the task and doesn't deliver the tap here. This call can't count a tap twice, because the SDK deduplicates it by message ID. The SDK can't detect notifications that your app draws from a data message, or push delivered outside FCM. Capture those yourself: ```kotlin PostHog.capturePushNotificationOpened( title = title, body = body, payload = message.data, ) ``` From Android SDK 3.65.0, this call skips a notification that PostHog sent if the SDK captured it in the last 5 minutes. ## Identity verification If your channel uses [identity verification](https://posthog.com/docs/workflows/push.md#verify-device-identity), return a token from your backend through `pushIdentityProvider`. Call `completion` exactly once: ```kotlin config.pushIdentityProvider = { distinctId, appId, completion -> // Get a token for this user from your backend, then: completion(token) // or completion(null) to send without one } ``` ## Troubleshooting | Problem | What to check | | --- | --- | | The token never registers | `firebase-messaging` is on the classpath, `google-services.json` is in place, and you forward `onNewToken`. | | A push never arrives | The channel's Firebase project matches your app, and the user granted `POST_NOTIFICATIONS`. | | Taps while the app runs aren't captured | Your launcher activity uses `singleTop` and forwards `onNewIntent`. | For problems that apply to every SDK, see [push troubleshooting](https://posthog.com/docs/workflows/push.md#troubleshooting).