Uma Achanta Uma Achanta - 3 months ago 70
Android Question

How to handle the fire base notification when app is in foreground

I have integrated Fire base cloud messaging with my application.
When I was sending notification from firebase console, If app is in background/not opened then I am receiving notification,

but unable to receive notification when app is in foreground/open

all suggestions are appreciated.

Answer

When app is in foreground,notifications are not generated themselves.you need to write some additional code.when message is received onMessagereceived method is called where you can generate the notification.here is the code:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
    NotificationCompat.Builder builder = new  NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("test")
            .setContentText(remoteMessage.getData().get("message"));
NotificationManager manager = (NotificationManager)     getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());

}

}
Comments