前台 Firebase 通知

7

我遇到了FireBase推送通知的问题。当我的应用程序在后台时,通知会出现,但是当我的应用程序在前台时,我不会收到通知,但是在控制台中显示该通知,这意味着通知已经到达,但它没有显示在通知栏中。你能帮我解决吗?

@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());
}
3个回答

5

编辑:

我刚刚注意到,当您从控制台发送通知并且您的应用程序在前台时,FirebaseMessagingService的onMessageReceived()方法不会被调用(截至目前)。一种解决方案是使用Firebase APIs发送推送通知,无论应用程序处于前台还是后台,它都将调用onMessageReceived()方法。

如果您使用 "notification"正文发送推送通知,即

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

要在onMessageReceived()中检索此内容,请使用以下内容:

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

如果您的应用程序在后台运行,Firebase将显示与推送中设置的数据相对应的通知,并调用onMessageReceived()。因此,如果您还编写了用于在onMessageReceived()中显示自定义通知的代码,则会看到2个通知。

为解决此问题,您可以使用类似以下的一些“data”键:

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

要在onMessageReceived()中获取它,请使用以下代码:

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

你可以构建自己的自定义通知,然后仅在onMessageReceived()中显示该通知。
顺便说一句,如果有帮助请点赞。我是stackoverflow的新手。

你有没有注意到sendNotification(RemoteMessage remoteMessage)使用RemoteMessage,但remoteMessage.getNotification().getBody()实际上返回的是String。你给了错误的建议。 - Yazon2006

2
您说您正在接收消息,因为您在控制台中看到了日志消息,因此您已经正确处理了FCM部分,问题可能出在您创建通知方面。
如果您查看通知创建代码,您会发现缺少Android上创建通知所需的一些必要元素。从文档中可以得知:
一个 Notification 对象必须包含以下内容:
  • 通过 setSmallIcon() 设置的小图标
  • 通过 setContentTitle() 设置的标题
  • 通过 setContentText() 设置的详细文本
因此,我认为如果您将这些项目添加到您的通知创建中,您应该会在通知区域看到通知。

是的,那是一个问题,谢谢伙计。还有一个问题,当我的后端发送通知并在JSON中设置标题时,如何设置setContentTitle()? - Goran_1992
通过通知消息(json)显示的通知完全与您在代码中创建和显示通知的操作是分开的,就像您在这里所做的一样。如果您使用通知消息并且您的应用程序在后台,则将显示指定的json。否则,您将收到onMessageReceived回调,并且由您的代码决定是否显示通知。 - Arthur Thompson

-1

Intent intent = new Intent(this, "你的类名"); 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());

工作对我来说很好。 - VISALIG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接