How to handle Firebase notification when an application is in the foreground

I integrated Firebase Cloud Messaging with my application. When I sent a notification from the Firebase console, if the application is in the background or not open, I receive a notification successfully, otherwise, if the application is in the foreground or open, I did not receive it.

All suggestions are welcome.

+19
source share
3 answers

When the application is in the foreground, notifications are not generated by themselves. You need to write additional code. When you receive the onMessageReceived() message, the onMessageReceived() method is where you can generate a 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")); 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); String channelId = "Default"; NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(remoteMessage.getNotification().getTitle()) .setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);; NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT); manager.createNotificationChannel(channel); } manager.notify(0, builder.build()); } } 
+44
source

FirebaseMessagingService never works when the application is in the foreground. In this case, if you want to receive a message from FCM, then WakefulBroadcastReceiver will work for you

 public class FirebaseDataReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("BroadcastReceiver::", "BroadcastReceiver"); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(intent.getExtras().getString("title")) .setContentText(intent.getExtras().getString("message")); NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); manager.notify(0, builder.build()); } } 

Associate firebase with GCM in the play store and write the code below in the manifest

 <receiver android:name=".firebase.FirebaseDataReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </receiver> 
+2
source

Follow this link, this is the easiest way to handle Firebase notifications by the application, and not the server, when the application is in the foreground and background with the help of a postman.

Firebase notifications in the background and foreground in Android

0
source

All Articles