Set up push notifications

Send push notifications to your iOS and Android apps from automations and broadcasts. Setup has three parts:

  1. Connect a push channel with your Firebase or Apple credentials.
  2. Register devices with the PostHog mobile SDK.
  3. Send a push from a broadcast or an automation.

Your app still owns its platform push setup. You turn on push with Firebase or Apple, and you ask the user for notification permission. The PostHog SDK reads the device token that your app gets, stores it on the person in PostHog, and captures an event when someone opens a notification.

Connect a push channel

Pick the provider that your app sends through:

  • Firebase Cloud Messaging (FCM) for Android, and for iOS apps that send through Firebase.
  • Apple Push Notifications (APNs) for iOS apps that send to Apple directly.

On the Channels tab, click Connect on the provider's card. To connect a channel, change it, or change its identity verification, you need project admin access.

The Channels tab with the Firebase Cloud Messaging and Apple Push Notifications cards and their Connect buttons

Firebase Cloud Messaging

Paste your Firebase service account key into Service account key JSON. To create a key, open the Firebase console and go to Project settings > Service accounts > Generate new private key. PostHog reads your Firebase project ID from the key.

The Configure Firebase Cloud Messaging form with the service account key field and the Identity verification selector

Apple Push Notifications

Create an APNs auth key in your Apple Developer account, under Certificates, Identifiers & Profiles > Keys. Then fill in the form:

FieldWhat to enter
Signing key (.p8)The key file you downloaded from Apple. Paste its contents or upload the file.
Key IDThe ID of the signing key.
Apple team IDYour Apple developer team ID.
Bundle IDYour app's bundle identifier, such as com.example.app.
EnvironmentProduction for App Store and TestFlight builds. Sandbox for builds that you run from Xcode.

To send to both kinds of builds, connect the same app twice, once for each environment.

The Configure Apple Push Notification Service form with all five fields and the Identity verification selector

Both forms also have an Identity verification setting. Keep it on Disabled until your app sends identity tokens. See verify device identity.

Register devices

Follow the guide for your SDK:

Every SDK registers devices the same way:

  • It registers automatically. The SDK registers the device token under the current distinct ID, so the device moves to the user when you call identify(). When you call reset() at sign-out, the SDK removes the device from the signed-out user and registers it under the new anonymous ID.
  • It sends an app ID with each device. The app ID is your Firebase project ID for FCM, or your bundle ID for APNs. PostHog stores the device only when a channel in the project has that app ID, and drops it otherwise. Connect the channel first. Devices that start before you connect it register when the SDK next loads its config.
  • It retries. If the device is offline or a request fails, the SDK tries again on the next flush, identity change, or app launch.

iOS apps that send through Firebase

If your iOS app sends through FCM, turn off automatic registration and register the FCM token yourself, with your Firebase project ID as the app ID. If you leave automatic registration on, the SDK also registers the APNs token under your bundle ID. The device then has two registrations, and a push that uses both channels arrives twice.

Devices from another provider

The SDK registers a device when a user opens a version of your app that includes it. To reach devices that registered with your old provider before then, import your device tokens.

Track opens

When someone taps a notification, the SDK captures a $push_notification_opened event. It can include $notification_title, $notification_subtitle (iOS only), and $notification_body, plus $notification_action when the person taps an action button.

  • On iOS, the SDK adds the title, subtitle, and body only for notifications that PostHog sent.
  • On Android, an open that the SDK captures automatically has no title or body, because Firebase removes them from the intent that it gives your app.
  • Automatic capture can't see notifications that your app schedules or draws itself. Capture those with the SDK's manual capturePushNotificationOpened call.
  • In recent SDK versions, the manual call skips a notification that PostHog sent if the SDK already captured the same tap in the last 5 minutes. Each SDK guide gives the version. Opens of notifications from other providers are never deduplicated.

To keep notification content out of your events, see protect sensitive data.

Verify device identity

A device token says where to deliver a notification. It doesn't say who owns the device. Your project token is public, so without identity verification, anyone who has it can register a device for any distinct ID.

With identity verification, your backend signs a short-lived token for the signed-in user. The app sends that token when it registers or unregisters the device, and PostHog checks it.

  1. Generate an EC P-256 key pair once. Keep the private key on your backend:

    Terminal
    openssl ecparam -name prime256v1 -genkey -noout -out push-identity-private.pem
    openssl ec -in push-identity-private.pem -pubout -out push-identity-public.pem
  2. On your backend, sign an ES256 JWT with the private key. PostHog accepts only ES256. The token needs these claims:

    ClaimValue
    subThe user's distinct ID
    app_idThe app ID that the SDK sends: your Firebase project ID or your bundle ID
    audposthog:push_identity
    expAn expiry time a few minutes ahead. PostHog rejects a token without one.

    For example, in Node.js with jsonwebtoken:

    JavaScript
    import jwt from "jsonwebtoken"
    const token = jwt.sign(
    { sub: distinctId, app_id: appId, aud: "posthog:push_identity" },
    privateKeyPem,
    { algorithm: "ES256", expiresIn: "5m" }
    )
  3. In your app, return the token from the SDK's pushIdentityProvider callback. Each SDK guide shows the syntax. The SDK keeps the token in memory for each distinct ID and app ID, and asks again when the identity changes or PostHog rejects the token. If your callback doesn't answer in 10 seconds, the SDK sends the request without a token.

  4. Edit the channel. Paste the contents of the public key file into Public key, and set Identity verification to Optional. PostHog checks each token, but still registers devices without a valid one.

  5. When your app sends valid tokens, set Identity verification to Required. PostHog then rejects a registration or unregistration without a valid token, with a 401.

Send a push

Each person gets the notification on the devices that they registered with the channels you pick. PostHog skips people who have no registered device. See limits for how many channels and devices one send reaches.

Pricing

A push step that reaches at least one of a person's devices counts as one dispatch under push and other destinations, however many devices it reaches. A push that reaches no device isn't billed.

PostHog stores each device on its person with a $set event. Each registration, and each imported device, counts as one Product Analytics event. See pricing.

Troubleshooting

ProblemWhat to check
The device never registersThe user granted notification permission, and your app registers for push with Firebase or Apple. The channel's Firebase project ID or bundle ID matches your app.
Registration fails with a 401The channel requires identity verification. Your pushIdentityProvider must return a token in 10 seconds, signed with the key that pairs with the channel's public key, with the correct sub and app_id, and not expired.
A push never arrives on iOSThe APNs channel's Environment matches your build, and its Bundle ID matches your app.
An iOS device gets each push twiceThe device registered with both your APNs and FCM channels. See iOS apps that send through Firebase.

Still have questions?

Was this page useful?