Push Notification Using The Android Studio And Google Firebase


This article demonstrates how to add push notification authentication on Android applications using Android Studio. 

Firebase Cloud Messaging

FCM is a cross-platform messaging solution that lets you reliably deliver the messages at no cost. Firebase is only for mobile platforms and allows you to quickly develop a quality application.

Message Type

FCM sends two types of message to the user:
  • Notification Message or Display Message.
  • Data Message handles the client app.

Notification Message

FCM automatically sends the message to the end-user. Use the Admin SDK or FCM Server protocols and set the notification key.

Data Message

The client app is responsible for processing data messages. Data messages only have custom key-value pairs. Use the Admin SDK or FCM Server protocols and set the data key only.
Step 1
Create a new project in Android Studio from File >> Project and fill in all the necessary details.
Step 2
Create a new project in Firebase console.
Enter your application package name and (Nick Name and Debug signing certificate SHA-1) Optional.
You will download a Google-services.json file that just moves the downloaded google-services file into an android app. 
Step 3 
Next, go to Gradle Scripts >> build.gradle (Module: app).Select build.gradle, The app Gradle compiles the code, and then build types will appear. Just replace that with the following code. Add the Firebase Core and Firebase Message Gradle in your project or you can also use Gradle. 
Dependencies Class
  1. classpath 'com.google.gms:google-services:3.2.0'  
Gradle for App  
  1. dependencies {  
  2.     compile 'com.google.firebase:firebase-core:12.0.0'  
  3.     compile 'com.google.firebase:firebase-messaging:12.0.0'  
  4. }  
  5. apply plugin: 'com.google.gms.google-services'  
Step 4
Next, go to app >> Java >> package name >> New Java Class. Select the Java Class. Just use the following code.
MyFirebaseMessageService.Java Code
  1. package io.github.selvaraju_saravanan.notification;  
  3. import android.app.NotificationManager;  
  4. import android.app.PendingIntent;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.media.RingtoneManager;  
  8. import android.net.Uri;  
  9. import android.support.v4.app.NotificationCompat;  
  10. import android.util.Log;  
  11. import com.google.firebase.messaging.FirebaseMessagingService;  
  12. import com.google.firebase.messaging.RemoteMessage;  
  15. public class MyFirebaseMessagingService extends FirebaseMessagingService {  
  17.     private static final String TAG = "FirebaseMsgService";  
  19.     @Override  
  20.     public void onMessageReceived(RemoteMessage remotemsg) {  
  22.         Log.d(TAG, "From -> " + remotemsg.getFrom());  
  23.         Log.d(TAG, "Demo Notification Body -> " + remotemsg.getNotification().getBody());  
  24.         sendNotification(remotemsg.getNotification().getBody());  
  25.     }  
  28.     private void sendNotification(String messageBody) {  
  29.         Intent intent = new Intent(this, MainActivity.class);  
  30.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  31.         PendingIntent pendingIntent = PendingIntent.getActivity(this0, intent,  
  32.                 PendingIntent.FLAG_ONE_SHOT);  
  34.         Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
  35.         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)  
  36.                 .setSmallIcon(R.mipmap.ic_launcher)  
  37.                 .setContentTitle("C# Corner")  
  38.                 .setContentText(messageBody)  
  39.                 .setAutoCancel(true)  
  40.                 .setSound(soundUri)  
  41.                 .setContentIntent(pendingIntent);  
  43.         NotificationManager notificationManager =  
  44.                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  45.         notificationManager.notify(0, notificationBuilder.build());  
  46.     }  
  47. }  
Again, create a new Java class. 
Just replace the following change -- but this is my package name, and you can change your application package name.
MyFirebaseInstanceIDService.java code
  1. package io.github.selvaraju_saravanan.notification;  
  3. import android.util.Log;  
  4. import com.google.firebase.iid.FirebaseInstanceId;  
  5. import com.google.firebase.iid.FirebaseInstanceIdService;  
  7. public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {  
  8.     private static final String TAG = "FirebaseIIDServiceDemo";  
  9.     public String[] name;  
  11.     @Override  
  12.     public void onTokenRefresh() {  
  13.         String token = FirebaseInstanceId.getInstance().getToken();  
  14.         System.out.println("my firebase token " + token );  
  15.     }  
  16.     private void sendRegistrationToServer(String token) {  
  18.     }  
  19. }  
Step 5 
After that again go back to Console Firebase > Click OverView > Notification. Select Notifications and Click Get Started button.
I have already sent one message to my app.
Write the information to the textbox and select your application package name and click send message button.
Review your message and user segment -- you have successfully connected your app.
Step 6
Next, go to Android Studio and deploy the application. Select Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
Deliver the Message that you have deployed the application.


We have successfully created a push message application. Later we will discuss more cloud apps. 

Similar Articles