Integrations

Android Push Notifications in Kotlin: FCM, Users and Lifecycle Campaigns

A Kotlin guide to Android push notifications with Firebase Cloud Messaging and PushFire: request POST_NOTIFICATIONS, get the FCM token, register the device over REST with OkHttp, handle token refresh, and send lifecycle push and email.

EGErik Goins
Published
Updated
7 min read
Share article
Android Push Notifications in Kotlin: FCM, Users and Lifecycle Campaigns — PushFire blog, Integrations

TL;DR: Android push notifications in Kotlin need Firebase Cloud Messaging for the token and a backend that maps tokens to users. With PushFire, your app sends the FCM token to /register-device with a Public key, updates it in onNewToken, and calls /login-subscriber when the user signs in. Your server or a visual workflow then sends push and email by your own user ID. A native Android SDK is coming soon; this guide uses OkHttp and the REST API, which works today.

Native Android push notifications in Kotlin are well documented on the Firebase side, but the part after the token is up to you: storing it, linking it to users, and building campaigns. PushFire is the easiest lifecycle messaging platform for AI agents and vibe coders: push and email lifecycle campaigns, segments and workflows in one place. This guide covers the Android code end to end. For the strategy side, read what a lifecycle messaging platform does.

What you need

  • An Android app connected to a Firebase project, with google-services.json in the app module.
  • A PushFire project with your Firebase service account uploaded. Create one free at console.pushfire.app.
  • A Public key (pf_pk_...) for the app. It can register devices, log in subscribers and manage tags, nothing more. See Public and Secret keys.

Add Firebase Messaging, OkHttp and coroutines

Declare the permission and the messaging service in AndroidManifest.xml:

A small PushFire client in Kotlin

Every PushFire endpoint takes JSON wrapped in a data object and a Bearer key. This client uses OkHttp 4 and org.json, which ships with Android.

How to set up Android push notifications in Kotlin

The device flow has three steps: ask for permission on Android 13 and later, get the FCM token, and register it once. PushFire rejects a duplicate FCM token on /register-device, so save the returned deviceId and subscriberId.

In a real app, ask for the permission after the user sees why notifications help, for example after they follow a topic, not on first launch.

Handle FCM token refresh in onNewToken

FCM calls onNewToken when it issues a new token. Send it to /update-device with the saved device ID:

Identify the user after sign-in

A new install starts as an anonymous subscriber. After sign-in, link it to your own user ID so your backend can reach this person by that ID:

To segment users, call /add-subscriber-tag with the subscriberId, a tagId UUID copied from the console, and a value. Tags power segments and workflow conditions.

Who sends the push: the app or the server?

Keep the Secret key on a server. Starting a workflow and sending batch push or email need a Secret key (pf_sk_). A Public key (pf_pk_) gets HTTP 403 on those calls. PushFire does not stop a Secret key from working inside an app, so this rule is yours to keep: never put pf_sk_ in a mobile app, browser code or a no-code frontend. Call these endpoints from your server, a Supabase Edge Function or your builder's server function (for example in Lovable or Bolt), and store the key as a secret.

The server. Your Android app only registers the device and identifies the user, which is why a Public key is enough and is safe to ship in the APK. Sending a push, sending an email, and starting a workflow need a Secret key (pf_sk_), and a Secret key must never be in a mobile app, because anyone can extract strings from an APK. Your backend calls /send-batch-push-notifications with your own user IDs, a title, a description and an optional data object for deep links. PushFire finds every device for those users and delivers through FCM. For lifecycle campaigns, build a workflow in the console with push, wait, condition and email nodes, then start it from your server or schedule it. This split means you can change campaign copy and timing in the console without shipping a new build to Google Play. Server examples are in the Node.js guide.

Android lifecycle email from the same workflow

Many Android users turn notifications off. Because login-subscriber stores the email address, a workflow can branch: push to users with notifications on, email to the rest. Connect Resend or SMTP in the console and design a layout once.

Let your AI agent write the Kotlin

Working in Android Studio with an agent, or in Claude Code, Cursor or Codex? Install the PushFire skills so the agent uses the right endpoints and key types:

The PushFire MCP server is live, so an agent can also manage your campaigns directly.

Frequently asked questions

Is there a native PushFire SDK for Android?

A native Kotlin SDK is planned and not yet released. Today, use the REST API as shown here: one small OkHttp client covers device registration, token refresh, login and tags. The endpoints and payloads will stay the same when the SDK ships, so this code keeps working.

Do I need the POST_NOTIFICATIONS permission?

Yes, on Android 13 (API 33) and later. Without it, the system does not show your notifications. Declare it in the manifest and request it at runtime. On Android 12 and earlier, notifications are allowed by default, so you can register the device right away with pushNotificationEnabled set to true.

Which API key goes in my Android app?

Only a Public key that starts with pf_pk_. The server limits it to registering devices, logging in subscribers and managing tags. Sending push or email and starting workflows require a Secret key, which must stay on your backend. Strings in an APK are easy to extract, so never ship a Secret key.

What should onNewToken do?

Send the new token to PushFire with /update-device, using the device ID saved at registration. Do not call /register-device again for the same install, because PushFire rejects duplicate tokens. If the app has no saved device ID yet, skip the update and let the normal registration flow run.

Can I send Android push notifications from my own backend?

Yes. Your backend, in any language, calls /send-batch-push-notifications with a Secret key and your own user IDs. PushFire delivers to each user's Android devices through Firebase Cloud Messaging, along with their iOS and web devices. The response tells you which user IDs were queued and which failed.

Start sending Android push notifications

Create a free project at console.pushfire.app. The Free plan covers up to 5,000 subscribers with push workflows. Upload your Firebase service account, add a Public key to your app, and send a test push from the console. The Android guide and the API reference cover every field.

Android Push Notifications in Kotlin: FCM Guide | PushFire