Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle the Firebase notification when app is in foreground

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

All suggestions are appreciated.

like image 496
Uma Achanta Avatar asked Jul 19 '16 06:07

Uma Achanta


People also ask

How do you handle notifications when an app is in foreground in Firebase flutter?

Foreground and Notification messages Notification messages which arrive while the application is in the foreground will not display a visible notification by default, on both Android and iOS. It is, however, possible to override this behavior: On Android, you must create a "High Priority" notification channel.

How do you handle the Firebase notification when an app is in foreground react-native?

So as a solution you can use react-native-push-notification to fire push notification when app in foreground. For android you don't need to follow any native installation steps just install library by this command and then you can fire local push notification as below : Create a file called NotificationController.

How do I get notification when an app is in foreground Android?

If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback. For an explanation of the difference between notification and data messages, see Message types.


2 Answers

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"));         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());     } } 
like image 106
Shivam Bhusri Avatar answered Sep 26 '22 19:09

Shivam Bhusri


FirebaseMessagingService never work when the app is in foreground. In this case if you want to receive the 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());         }     } 

Link firebase with GCM in play store and write the below code in 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> 
like image 42
Ramana V V K Avatar answered Sep 24 '22 19:09

Ramana V V K