Wednesday, 24 January 2018

Firebase Push Notification Implementation

Step 1:- Add a new project in Android studio.

Step 2:- Now to use Firebase Push Notification we have to open Firebase console  into your browser.

Step 3:- If u are not login than login with your Gmail account firstly and than open this link.


Step 4:- Now click on Add Project -> than one popup will open ->

Step 5:- In project name give your project name and select your country like following:-

Step 6:-Now click on CREATE PROJECT button-> than one screen will be open above the screen u can see your project name .Now click on Android icon to add Firebase to Android application.

Step 7:- Now following screen will be open with one popup:-

Step 8:- In  the package name field give your package name of your application in which you want to add Firebase notification, like following:-

Step 9:- Next click on REGISTER APP and go forward. Here u have to follow the instruction. Like download  google-services.json file


Step 10:- After download the file ,copy the file -> open android studio -> select Application view type as Project -> and click on app name-> inside  app right click and paste the file :-


Step 11:-  Add some gradle properties :-

Step 12:- now in Module:app Gradle file add one more line like following
inside dependencies{} block and than sync the project.
compile 'com.google.firebase:firebase-messaging:11.8.0'

Step 13:- Now right click on Java->add new class with class name  MyFirebaseInstanceIdServiceClass code will be like following :-

public class MyFirebaseInstanceIdServiceClass extends FirebaseInstanceIdService{
    private static final String regToken="REG_TOKEN";
    @Override    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(regToken,refreshToken);
        
    }
}
Step 13:- Now again add one more class with name MyFirebaseMessagingServiceClass code will be following :-

public class MyFirebaseMessagingServiceClass extends FirebaseMessagingService {
    @Override    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("My FCM push notification Example");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
}
Step 13:-Now open Manifest file and add the services like following
<service android:name=".MyFirebaseInstanceIdServiceClass">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
    </intent-filter>
</service>
<service android:name=".MyFirebaseMessagingServiceClass">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"></action>
    </intent-filter>
</service>

Step 13:-Now run the application and from logcat copy the Token id:- it will be like following

Step 14:-Now go to firebase console go to your project there search for Notification and click on that it will be like following:-


Step 15:-Click on SEND YOUR FIRST MESSAGE

Step 16:- now fill following and



Step 17:- Click on SEND MESSAGE -> SEND after this u will get the notification. you will see the notification like following

2 comments:

  1. I was looking something like this, really simple to understand and follow, thank you!

    ReplyDelete