Guide ·

Flutter Push Notifications with Firebase Cloud Messaging and a Node.js Backend

How to add push notifications to a Flutter app with Firebase Cloud Messaging and your own Node.js API: tokens, permissions, sending, deep links and testing.

By Ritesh Singh, full-stack developer

Almost every business app needs push notifications: a hearing date tomorrow, an order that shipped, a payment that failed, a show that just went live. In a Flutter app the delivery layer is almost always Firebase Cloud Messaging (FCM), which reaches Android directly and iOS through Apple's push service. What makes them useful is your own backend: who gets notified, about what, and what happens on a tap. This guide covers how I set it up with a Flutter app and a Node.js API.

1. Decide what the backend owns

Let the Node.js API own three things: the list of device tokens per user, the rules for who should be notified about each event, and a log of what was sent. The Flutter app's job is smaller: ask for permission, hand its token to your API, show notifications, and open the right screen when one is tapped. The web dashboard and the app then follow the same rules, as in my guide on one API for a Flutter app and web dashboard.

2. Set up Firebase for both platforms

Create the Firebase project under the client's own Google account, add the Android and iOS apps with their final package name and bundle ID, and run flutterfire configure to generate the config. Add the firebase_core and firebase_messaging packages.

iOS needs two extra steps that are easy to miss. In Xcode, enable the Push Notifications capability and Background Modes with remote notifications. Then create an APNs authentication key (a .p8 file) in the Apple Developer account and upload it to the Firebase project settings. Without that key, iPhones get a token but never receive anything.

3. Ask for permission at the right moment

iOS has always required permission, and Android 13 and later do too. Call FirebaseMessaging.instance.requestPermission(), but not on first launch: ask when the value is obvious, such as when the user turns on alerts for an order. On iOS you usually get one chance before the user has to go to Settings.

4. Register device tokens with your API

After login, get the token with getToken() and send it to an authenticated endpoint such as POST /v1/devices. Listen to onTokenRefresh and send the new token whenever it changes, because tokens do change after reinstalls, restores and updates.

On the server, keep a device_tokens table: user ID, token (unique), platform, app version and a last-seen time. One user often has several devices, so never store a single token on the user row. Upsert by token, so a phone that changes hands moves to the new user. On logout, delete that device's token, or the next person to use the phone may see the previous user's alerts.

5. Send from Node.js with the HTTP v1 API

Google shut down the old legacy FCM API with its server key. Use the HTTP v1 API, which authenticates with a service account. The simplest way from Node.js is the firebase-admin package: load the service account from an environment variable or secrets store, never from the repository, and call getMessaging().send() for one device or sendEachForMulticast() for up to 500 tokens.

Do not send inside the HTTP request that triggered the event. When an order ships, write a notification job to a queue or a jobs table and return immediately; a worker picks it up, looks up the tokens and sends. That keeps your API fast and lets you retry failed sends safely.

6. Notification messages vs data messages

An FCM message can carry a notification block (title and body that the operating system shows by itself when the app is in the background) and a data block (key-value pairs your app reads). For most business apps, send both: the notification block for the visible alert, and data such as {"type": "order", "id": "8421"} so the app knows where to go.

Keep the payload small (the limit is 4 KB) and keep sensitive details out of it. Notifications appear on lock screens, so "Your payment status changed" is better than the amount and account number. The app can fetch the details from your API after the tap, behind a normal login check.

7. Handle all three app states in Flutter

  • Foreground: FirebaseMessaging.onMessage fires, but Android does not show anything by itself. Show a local notification with flutter_local_notifications, or an in-app banner. On iOS, setForegroundNotificationPresentationOptions decides whether the system shows it.
  • Background: the system shows the notification; onMessageOpenedApp fires when the user taps it.
  • Terminated: check getInitialMessage() at startup, because the app was launched by the tap.

If you need to run code when a message arrives in the background, register a handler with FirebaseMessaging.onBackgroundMessage. It must be a top-level function marked @pragma('vm:entry-point'), and it runs without UI, so keep it light.

Route every tap through one function that reads the type and id from the data block and navigates, whether the tap came from the foreground, background or terminated state. If the user is logged out, send them to login first and continue to the target screen afterwards. If the record is gone or access was removed, show a clear message, not a blank screen.

9. Android channels, topics and preferences

On Android 8 and later every notification belongs to a channel, and users can mute channels one by one. Create a few meaningful channels, such as "Reminders", "Orders" and "Announcements", so a user who mutes announcements still gets reminders. Mirror them in your settings screen and check preferences on the server before sending.

For broadcasts to everyone, such as a radio show going live, use FCM topics: the app subscribes and the server sends one message to the topic. Personal alerts still go to individual tokens.

10. Clean up tokens and log every send

When a send fails because the token is no longer registered or is invalid, delete that token. Otherwise the table fills with dead devices. Log each send with user, type, record ID, status and time, so "I never got the reminder" takes a minute to answer.

11. Testing before release

Test on real Android and iPhone devices, in all three app states, with a release build as well as a debug build. Prove the setup with a Firebase console test message, then trigger real events through your API. Check the lock screen, a logout and a reinstall too. Store review teams also notice notification prompts that appear without context, which I cover in my guide on publishing a Flutter app on the Play Store and App Store.

Checklist

  • Firebase project in the client's account; APNs key uploaded for iOS.
  • Permission asked in context, not on first launch.
  • Tokens stored per device, upserted by token, refreshed, and deleted on logout.
  • Sending uses the HTTP v1 API through firebase-admin, with credentials outside the repository.
  • Notifications are sent by a background worker, not inside the API request.
  • Payloads are small and free of sensitive data; details load after the tap.
  • Foreground, background and terminated taps all go through one router.
  • Android channels and user preferences decide what gets sent.
  • Invalid tokens are removed and every send is logged.

Need push notifications in your Flutter app, or the backend that decides when to send them? See my Flutter app development service and my Node.js and PHP backend development service.

Related services

Hire me for this

Want this done for your product?

Start your project →