Firebase Cloud Messaging (FCM) Android Example and Tutorial

 

       In this Firebase Cloud Messaging tutorial, we will see how to send push notification to android device using Firebase Cloud Messaging. So let’s begin.

Why FCM over GCM?
        Previously Google has its messaging service named GCM (Google Cloud Messaging). But then there are some grey area reasons to switch to FCM.

1. Google is investing in Firebase, making it its unified mobile platform. Over the last few years, Google have expanded GCM to send messages to multiple platforms beyond Android: iOS and Chrome. Firebase is known for being cross platform, so FCM now makes a natural fit in the Firebase suite of features designed for Android, iOS, and mobile web.

2. With FCM we have Firebase console available to send push notifications on a click; in addition to FCM sever APIs that can be called from backend server to send push notifications.

3. The registration token lifecycle is very much simplified with FCM.

So now we have good reason to learn FCM, let’s start configuring FCM for your project. This is one-time process.

Step 1 : Create simple Android project in Android studio.
Step 2 : Get your package name from AndroidManifest.xml. Mention user permission for Internet access in AndroidManifest.xml file.


Step 3 : Go to firebase console and create a new project.


Step 4 : Window will appear as below. Give a project a name, select your country and click on “CREATE PROJECT” BUTTON


Step 5 : Now click on Add Firebase to your Android App.



Step 6 : Mention package name that you got from step 2.



Click on “ADD APP” button. google-services.json file will get downloaded; add to your project as shown below.


Click on “CONTINUE” button. Follow step as shown in figure and click FINISH.


Now we are ready with setup. Lets start with Implementing Firebase Cloud Messaging
Create a class “MyAndroidFirebaseMessagingService.java” extending “FirebaseMessagingService”

MyAndroidFirebaseMessagingService.java

package fcm.android.com.firebasecmdemo;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;


public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// creating notification

Intent i= new Intent(this, TargetActivity.class);
PendingIntent pi= PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder bn= new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("CTE FCM Tutorial")
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel( true )
.setContentIntent(pi);
NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, bn.build());
}
}

In above code we have created PendingIntent for opening TargetActivity when user clicks on received notification.

Simplest TargetActivity.java can be as follow:

TargetActivity.java

package fcm.android.com.firebasecmdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class TargetActivity extends AppCompatActivity {

private TextView textView ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.message);
textView.setText("The Target Message Can Be Shown Here ");
}
}


Now we have to define the above service in our AndroidManifest.xml file. So go to manifest and modify as follows.


Complete AndroidManifest.xml is as below:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fcm.android.com.firebasecmdemo">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".TargetActivity">

</activity>

</application>

</manifest>


That’s all. Your app is ready to receive notifications.

Sending Push Notification using Firebase Console

Go to firebase console and select the app you created.
From the left menu select notification.
Click on new message.


Set the message, select your application package from dropdown as shown in below.
Click SEND MESAAGE.



Those who have installed your application will get the push notification.



Download Complete Project here

No comments:
Write comments

Popular Posts