Docs

Android push notifications in Kotlin

Integrations

This guide adds Android push notifications in Kotlin with PushFire. Firebase Messaging gives your app an FCM token. Your app registers that token with the PushFire REST API, identifies the signed-in user and sets tags. Your backend then sends push and email, and your workflows run in PushFire. PushFire is the easiest lifecycle messaging platform for AI agents and vibe coders: push and email lifecycle campaigns, segments and workflows behind one small API.

A native Android SDK is coming soon. Until it ships, the app calls the REST API directly with OkHttp, as shown below. The calls are the same ones the Flutter and Swift SDKs make. The full contract is in the API Reference.

Prerequisites

  • A PushFire account and project at console.pushfire.app, with your Firebase service account uploaded. See Create your first project.
  • A Public key (pf_pk_...) from Developers → API keys. A Public key can register devices, identify subscribers and manage tags. It cannot send push or start workflows, so it is safe to ship in an app. See Public and Secret keys.
  • An Android app in the same Firebase project, with google-services.json in the app/ folder.
  • Android Studio, Kotlin and a device or emulator with Google Play services.

Install Firebase Messaging and OkHttp

Add the Google services plugin and the dependencies. Use the latest Firebase BoM version from the Firebase release notes.

Declare the notification permission and a messaging service in the manifest. Android 13 and later need POST_NOTIFICATIONS at runtime.

Initialize a small PushFire client

Every endpoint lives under https://api.pushfire.app/functions/v1. Every request sends Authorization: Bearer <key> and wraps its fields in a data object. Network calls run on Dispatchers.IO.

Register the device with its FCM token

Register once, store the returned deviceId and subscriberId, and update the same device when the token changes. PushFire rejects a second registration of the same FCM token, so never register twice.

Send every device field. The API requires the keys osVersion, appVersion, manufacturer, model and language. Each may be JSONObject.NULL, but a missing key returns a 400.

Ask for the permission on Android 13 and later, then sync so PushFire stores the real pushNotificationEnabled value.

FCM rotates tokens. Forward each new token to the same device.

Identify the subscriber

When the user signs in, log the subscriber in with your own user ID as externalId. PushFire replaces the anonymous subscriber on this device and returns the identified subscriber's ID. TimeZone.getDefault().id gives an IANA zone such as America/New_York, which "At time" wait nodes use.

Login ignores metadata. To store custom profile fields, call PATCH update-subscriber with the subscriber id and a metadata object.

Add, update and remove tags

Create the tag in Audience → Tags, then use its UUID. A value is a string, a number of 1 or more, or null. Use add for a tag the subscriber does not have yet, and update to change its value.

Note that JSONObject.put(name, null) removes the key, so check for missing IDs before you build a request.

Trigger a workflow and send push from your server

The Public key in the app cannot start workflows or send messages. The API answers 403 "Client key cannot perform this action". Do not work around this with a Secret key in the app.

Warning: never ship a Secret key (pf_sk_) in a mobile app, browser bundle or no-code frontend. PushFire does not block a Secret key on a client, so the call works, but anyone can extract the key from your app and message all your users. Start workflows and send messages from your server, a Supabase Edge Function or your app builder's server function.

Call these endpoints from your backend with a Secret key (pf_sk_...). Batch push targets your externalId values. A workflow targets PushFire subscriber UUIDs or segment UUIDs.

Often you need no server call at all. A workflow with the "A subscriber enters a segment" start runs by itself when the app's tags move a subscriber into that segment. See Workflows.

For full server code, see Node.js or Python. Email works the same way through send-batch-email-notifications.

Verify delivery

  1. Run the app on a device, allow notifications and sign in.
  2. Open Audience → Subscribers in the console. Find your externalId and check the Android device, push status and tags.
  3. Put the app in the background and send the curl request above.
  4. Check the notification in the tray, then open Developers → API Messages → Push Notifications for the status.

Troubleshooting

  • 400 with "path": "data.model" (or another device field): a device key is missing. Send it with JSONObject.NULL.
  • 400 "Device with FCM token already exists": the app registered twice, often after a reinstall that cleared prefs but kept the token. Clear app data or call FirebaseMessaging.getInstance().deleteToken() and register again.
  • No notification on Android 13 and later: the user denied POST_NOTIFICATIONS. The device shows push disabled in PushFire. Send the user to the app's notification settings.
  • Notification arrives only in the background: this is normal. In the foreground, onMessageReceived runs and your app decides what to show.
  • 403 "Client key cannot perform this action": the app tried to send push or start a workflow with the Public key. Move that call to your server.
  • Delivery fails with a SenderId or auth error in API Messages: the Firebase service account in PushFire and the google-services.json in the app come from different Firebase projects.

FAQ

Is there a native PushFire Android SDK?

Not yet. A native Android SDK in Kotlin is planned and listed as coming soon. Today you call the PushFire REST API with OkHttp, as this guide shows. The code is about one hundred lines, and it keeps working after the SDK ships because the SDK will call the same endpoints.

Is it safe to put the PushFire key in my Android app?

Use only the Public key (pf_pk_) in the app. Anyone can extract it from the APK, so PushFire limits it to identity actions: register devices, identify subscribers and manage tags. It cannot send push, send email or start workflows. Keep the Secret key on your server.

Does PushFire replace Firebase Cloud Messaging on Android?

No. PushFire delivers through FCM with your Firebase project. Firebase Messaging still gives the app its token and shows the notification. PushFire adds subscribers, tags, segments, visual workflows, email and delivery tracking on top, so you do not build that backend yourself.

What happens when the FCM token changes?

Firebase calls onNewToken in your messaging service. Send the new token to PushFire with PATCH update-device and the stored device ID, as sync() does. Do not call register-device again, because PushFire would create a second device and anonymous subscriber.

Can I use Jetpack Compose with this setup?

Yes. Nothing here depends on the UI toolkit. Call sync() from your activity or a ViewModel, and call the sign-in and tag functions from your Compose event handlers inside a coroutine scope such as rememberCoroutineScope() or viewModelScope.

Next steps