A Step-by-Step Guide to Implementing Push Notifications with FCM

Firebase Cloud Messaging (FCM) is a free, reliable cross-platform messaging solution that allows you to send push notifications to Android, iOS, and web apps. This tutorial covers the core steps to get your notification system up and running quickly.

Start by creating a Firebase project in the Firebase Console. Add your Android or iOS app to the project and download the google-services.json (Android) or GoogleService-Info.plist (iOS). Add the FCM dependency to your app-level build.gradle or Podfile. Note your Server Key from Project Settings > Cloud Messaging — you’ll need it for server-side requests.

Article illustration

1. Client-Side Registration

In your app, obtain the FCM registration token by calling FirebaseMessaging.getInstance().getToken(). This unique token identifies the device. Send this token to your backend server and store it for later push targeting.

2. Sending Notifications from Your Server

Use the FCM HTTP v1 API (recommended) or legacy API. With the v1 API, you need OAuth 2.0 credentials — the easiest way is to use the Firebase Admin SDK in Node.js, Python, or Java. For a quick test, you can also use the Firebase Console’s “Send Notification” panel. The payload should include notification (title, body) and optional data fields.

3. Handling Notifications on the Device

FCM automatically displays notifications when the app is in the background. For foreground notifications, override onMessageReceived() in your custom FirebaseMessagingService to show a local notification or process data. On iOS, ensure you register for remote notifications and configure UNUserNotificationCenter.

4. Testing and Troubleshooting

Use the Firebase Console’s notification composer to send a test message to a specific token. Check logcat (Android) or Xcode logs for errors like “MismatchSenderId” (wrong server key) or “InvalidRegistration” (expired token). Always refresh tokens on onNewToken() and handle tokenRefresh callbacks.

With these steps, you have a basic push notification system using FCM. Extend it with topics, conditional targeting, and A/B testing to engage your users effectively. Monitor delivery rates in Firebase Analytics for ongoing optimization.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *