Send Notification Using Firebase Cloud Messaging In Android

How to send Notification Using Firebase Cloud Messaging in Android


Please Subscribe Youtube| Like Facebook | Follow Twitter

Introduction

In this article we will learn how to send Push Notification to your app user using Firebase Cloud Messaging (FCM) in Android. Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.

Our Example

In our example we will send test notification message to all our app user.

Requirements:

  1. Android Project/App in which Notification to be implemented
  2. Firebase Project

Note: First you need to create Firebase Project and then connect your app to it. You can follow this article to set it up.

Steps

Follow below steps

1) Integrate Firebase Cloud Messaging in your App

2) Run App and Use FCM at firebase console to send notification to your users

1) Integrate Firebase Cloud Messaging in your App

Add FCM library at app level build.gradle file

implementation 'com.google.firebase:firebase-messaging:20.2.0'

2) Run App and Use FCM at firebase console to send notification to your users

Run your app. Then close your app so that to receive notification when your app is in background or killed.

Now Open Firebase Console click on Grow->Cloud Messaging. And click on send your first message.

Then add notification title, text e.t.c

Select your app package name in target

Select now in Scheduling

Select Conversion event details (optional)

Add additional options (optional). And then click on review.

After review click on publish to send notification to your users

Notification will be displayed on notification panel of your mobile

Note: If you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, then you have to extend service FirebaseMessagingService.

Add service in your manifest file

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

And add below permission

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

And then create class that extends this service. onMessageReceived() will be called when message is received, handle your notification here.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
	//handle notification here
}

Conclusion

So in this post we have learned how to use Firebase Cloud Messaging to send push notification messages in Android.

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

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