Learn how to set up Firebase push notifications for Android, iOS, and Web with our easy, step-by-step guide to configure FCM and implement notification code.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Your Firebase Project
Step 2: Add Firebase to Your App
In the Firebase Console, select your project.
Click on the platform icon (iOS, Android, or Web) to add Firebase to your app. Follow the provided instructions based on your platform:
For Android:
Enter your package name and optional app nickname.
Download the google-services.json file and add it to your app directory.
Add Firebase SDK to your app by updating your build.gradle files:
// In your root-level (project-level) Gradle file (build.gradle), add:
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.10' // Check for the latest version
}
}
// In your app-level Gradle file (usually app/build.gradle), add:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
// Add the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics:19.0.0'
// Add the Firebase Cloud Messaging dependency
implementation 'com.google.firebase:firebase-messaging:23.0.0' // Check for the latest version
}
For iOS:
Download the GoogleService-Info.plist file and add it to your Xcode project.
Use CocoaPods to add the Firebase SDK. In your Podfile, include:
pod 'Firebase/Messaging'
Run pod install in your project directory.
For Web:
Add the Firebase SDK to your project by including the scripts in your HTML file:
</pre></code>
Step 3: Set Up Firebase Cloud Messaging (FCM)
Step 4: Implement Push Notification Code in Your App
For Android:
Extend FirebaseMessagingService to handle incoming FCM messages:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle FCM messages here.
// Not getting messages here? See why this may be: https://firebase.google.com/docs/cloud-messaging/android/receive
if (remoteMessage.getNotification() != null) {
// Get the message and handle it
String messageBody = remoteMessage.getNotification().getBody();
sendNotification(messageBody);
}
}
private void sendNotification(String messageBody) {
// Create and display the notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default\_channel")
.setSmallIcon(R.drawable.ic\_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION\_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default_channel", "Default Channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
Don't forget to declare MyFirebaseMessagingService in your AndroidManifest.xml:
For iOS:
Implement the messaging delegate methods in your App Delegate or any other initial view controller:
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(\_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Configure Firebase
FirebaseApp.configure()
// Set UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().delegate = self
// Request notification permissions
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
// Handle the authorization request response
}
application.registerForRemoteNotifications()
// Set the Messaging delegate
Messaging.messaging().delegate = self
return true
}
// Receive and handle FCM messages
func application(\_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Handle the message
}
// Handle notification settings
func userNotificationCenter(\_ center: UNUserNotificationCenter,
openSettingsFor notification: UNNotification?) {
// Handle the settings
}
// Handle FCM token refresh
func messaging(\_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: (String(describing: fcmToken))")
}
}
For Web:
Register a service worker and handle messages:
// Initialize Firebase
const firebaseConfig = {
// Your Firebase config here
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
messaging.getToken({ vapidKey: 'YOUR_PUBLIC_VAPID_KEY' }).then((currentToken) => {
if (currentToken) {
// Send token to server and display notification UI
console.log('Token:', currentToken);
} else {
console.log('No registration token available. Request permission to generate one.');
}
}).catch((err) => {
console.error('An error occurred while retrieving token. ', err);
});
// Handle incoming messages
messaging.onMessage((payload) => {
console.log('Message received. ', payload);
// ...
});
Step 5: Testing Push Notifications
This comprehensive guide should help you set up and implement Firebase push notifications for your Android, iOS, or Web app.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.