Android push notifications

Register Android devices for push notifications with the PostHog Android SDK, and capture when users open them. Before you start, connect an FCM 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 = "<ph_project_token>").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, 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

ProblemWhat to check
The token never registersfirebase-messaging is on the classpath, google-services.json is in place, and you forward onNewToken.
A push never arrivesThe channel's Firebase project matches your app, and the user granted POST_NOTIFICATIONS.
Taps while the app runs aren't capturedYour launcher activity uses singleTop and forwards onNewIntent.

For problems that apply to every SDK, see push troubleshooting.

Still have questions?

Was this page useful?