Firebase notifications in the foreground

2019-06-17 18:21发布

问题:

I am having problem with FireBase push notifications. When my app is in the background, notifications are coming, but when my app is in the foreground i don't receive notifications, but in console that notification is displayed, that means that notification is here but it doesn't show in notification bar . Could you help me?

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("OnMessage", "Received");
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    String aa=remoteMessage.toString();
    Intent intent=new Intent(MyFirebaseMessagingService.this, MainActivity.class);
    intent.putExtra("msg", aa);
    sendNotification(remoteMessage);
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setContentText(remoteMessage.getNotification().getBody());

    // remoteMessage.getNotification().getBody();

}


private void sendNotification(RemoteMessage remoteMessage) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setContentText(remoteMessage.getNotification().getBody())
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

回答1:

Like you said you are receiving the message since you are seeing the log messages in the console so you have handled the FCM part properly and the issue is likely with your creation of the notification.

If you look at your notification creation code you are missing a couple of the required elements to create a notification on Android. From the docs:

A Notification object must contain the following:

  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()

So I think if you add those items to your notification creation you should see the notification in the notification area.



回答2:

EDIT:

I just noticed that onMessageReceived() of FirebaseMessagingService is not invoked(as of now) when you send notifications from console and your app is in foreground. One solution is you can use Firebase APIs to send push notifications and it will invoke onMessageReceived() regardless of app being in foreground or background.

If you use "notification" body to send push notification i.e.

notification: {
  title: "notification title",
  icon: "ic_launcher",
  color: "#4E2AA5",
  body: "notification body"
}

To retrieve this in onMessageReceived(), use something like this:

String color = remoteMessage.getNotification().getColor();
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
String icon = remoteMessage.getNotification().getIcon();

If your app is in background, Firebase will display a notification corresponding to the data you set in push and will also invoke onMessageReceived(). Thus you will see 2 notifications if you have also written code to show custom notification in onMessageReceived().

To work around this, you can use some "data" key like this:

data: {
  title: "notification title",
  body: "notification body"
}

and to retrieve it in onMessageReceived(), use something like this:

Map<String, String> map = remoteMessage.getData();
for (Map.Entry<String, String> entry : map.entrySet()) {
   Log.d(TAG, entry.getKey() + "/" + entry.getValue());
}

You can then build your own custom notification and display it from onMessageReceived() only that notification will show up.

P.S. Please vote up if it helps. I am new to stackoverflow.



回答3:

Intent intent = new Intent(this, "your class"); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
           PendingIntent.FLAG_ONE_SHOT);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
        notificationManager.createNotificationChannel(notificationChannel);
        builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
    } else {
        builder = new NotificationCompat.Builder(getApplicationContext());
    }

    builder = builder
            .setSmallIcon(R.mipmap.ic_app_icon)
            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.picker_button_background_innactive))
            .setContentTitle((messageBody))
            .setTicker(title)
            .setContentText(message_body)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    notificationManager.notify(1, builder.build());