Skip to main content

Code#8 : Add Firebase Push Notification in your Android App


I hope you all are happy coding and living curiously to learn something new everyday. And like others me too want to make my work easier by creating some useful content which can help me and other programmers around the globe to easily access the code from anywhere and anytime.
Today we are going to learn how to add Firebase Push Notification Perk to your Android App and it is one of the peaceful implementation ever. Just Kodding.

Step 1: Sign in to your Gmail Account and Create a New Project in Firebase Console : https://console.firebase.google.com/

Step 2: Clicking on Add Project will pop this dialog up:
Step 3: Now click that green button in the middle for adding firebase to android app:
Step 4: Enter Package Name and SHA1 Key for step one:
Use this to get your SHA1 key — keytool -list -v -keystore C:\Users\user\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android
Step 5: Now Download the googleservices.json file and place in your Android Project.
Step 6: Now Add the below two lines in respective build.gradle files:
Imp Note: Add two java classes in your Android Project main package with preferably these names:
  • FireBaseInstanceIDService.java
  • FireBaseMessagingService.java
Now Add these codes in the respective files:
 — — — — — — — — — — — — — — — — — — — — — — — — — — — — — 
 FirebaseInstanceIDService Code:
 ===============================
public class FireBaseInstanceIDService extends FirebaseInstanceIdService{
private static final String TAG = “MyFirebaseIIDService”;
@Override
 public void onTokenRefresh() {
//Getting registration token
 String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token on logcat
 Log.d(TAG, “Refreshed token: “ + refreshedToken);
 }
 }
— — — — — — — — — — — — — — — — — — — — — — — — — — — — —
 FirebaseMessagingService Code:
 ===============================
public class FireBaseMessagingService extends FirebaseMessagingService implements Constants {
private static final String TAG = "MyFirebaseMsgService";
    private static int count = 0;
@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.d(TAG, "Notification Message TITLE: " + remoteMessage.getNotification().getTitle());
        Log.d(TAG, "Notification Message BODY: " + remoteMessage.getNotification().getBody());
        Log.d(TAG, "Notification Message DATA: " + remoteMessage.getData().toString());
//Calling method to generate notification
        sendNotification(remoteMessage.getNotification().getTitle(),
                remoteMessage.getNotification().getBody(), remoteMessage.getData());
    }
//This method is only generating push notification
    private void sendNotification(String messageTitle, String messageBody, Map<String, String> row) {
        PendingIntent contentIntent = null;
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(contentIntent);
NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(count, notificationBuilder.build());
        count++;
    }
 }
Another Imp Note: And register both these files in your Projects AndroidManifest.xml file in the <application> tag.
Step 7: And Now Go back to Firebase Console and Go to Notifications from Side Menu which will open this screen:
Step 8: Now just enter Notification Message and select your app package name to send the push.
Step 9: Hopefully if you have performed each step then you will get the push notification in your device.

So that’s it for this tutorial and blog. Hopefully it helps you somewhere along the line of your development phase, if yes the applaud the effort by doing some claps, it will boost me to share more. Just code, develop, learn and share the code to everyone, because once again, I have to say this:

<Sharing is Caring/>

Kudosss!!!

Comments

Popular posts from this blog

Let’s keep it short….again

Let’s keep it short….again The world is full of people who are full of Fear of Failure which doesn’t allow them to move steadily and think positively. But many of them don’t know that Fear is a Big Liar. Because Fear shows you fake reality about your own self that — ”You can’t do this thing”. Instead people should fake their fear by repeatedly saying this — ”At least I can try!!!” Because there is no sense at all to not do the particular thing just because you have never done it earlier. Try Again…Fail Again…Fail Better!!!😎 Kudossss To Fearless You…

Grateful 24/7/365

Grateful 24/7/365 The super basic problem with most of us is that we are not  grateful  for the life we all are living in currently. And the super duper solution for all of our this problem is to be  grateful enough for the life that we all got. Now to do that we can follow this simple phenomenon called -  Attitude of Gratitude. Many of us don’t even know How to be Grateful? So the best answer for this is: Becoming grateful and thankful for the smallest of things in your life is the best way to achieve the  Attitude of Gratitude. What to do with so much of Attitude of Gratitude? Many people even ask this silly but interesting question - that what we will get in return or what is the outcome of showing so much  gratefulness . So the best and interesting answer for this is: Keep yourself busy and focussed on your  attitude of gratitude , which will eventually make way for disappointments ...

That Winning Moment!!!

That Winning Moment!!! You are just a thought away from the fact that the penultimate thing we all want in our career is that winning moment and the motivation to carry that winning momentum all through outside and inside our career span. Let’s take a sneak peek of what can be some of the greatest winning moments. * That Bird who assembles the Nest: Have you ever thought about that little bird who is continuously collecting those tiny little wooden sticks to assemble or make home for her young ones to keep there and feed there. How great that  moment  will be for that bird, when she place that final wood stick on her assembled nest to finish that small yet hardworking activity. The bird can die for that type of winning moment!!! * That college boy who got placed in an interview: Everyone of you have definitely been through that phase of your graduation or school-life where you got selected for sitting in ...