Add FireBase Notification in Android App

Add MyFireBaseinstanceIDService.java class in your source
package com.yourpackage;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

import static android.content.ContentValues.TAG;

public class MyFireBaseinstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
// sendRegistrationToServer(refreshedToken);
}
}

Add MyFirebaseMessagingService.java class 

package com.yourpackage;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

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

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {



}
@Override
public void onMessageReceived(RemoteMessage remoteMessage)

{
super.onMessageReceived(remoteMessage);

sendNotification(remoteMessage.getNotification().getBody());
/* if(remoteMessage == null)
{
return;
}
if(remoteMessage.getNotification() != null)
{
handleNotification(remoteMessage.getNotification().getTitle());
}*/
}
/////////////////////////////////////////


public void sendNotification(String message)
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("yourappname")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationmanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationmanager.notify(0, notificationBuilder.build());
}

}

Add Following service in Mainifests file

<service android:name=".MyFireBaseinstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">

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

</service>

Now add dependencies in gradle

implementation 'com.google.firebase:firebase-messaging:11.8.0'

Now go to firebase console and login and create google-services.json and add it in your projects app folder .
Now Final step is to create app with your application package name.

Hence done 🙂
Happy Coding

Leave a Reply

Your email address will not be published. Required fields are marked *