Add Firebase Push Notification in your Android App

  • 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
  • Step 3: Now click that green button in the middle for adding firebase to android app:
  • Step 4: Enter Package Name and SHA1 Key
  • 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 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 tag
  • Step 7: And Now Go back to Firebase Console and Go to Cloud Messaging from Side Menu
  • Step 8: Now just enter Notification Message and select your app package name to send the push.