【问题标题】:Notifications not displayed when app is in foreground and onMessageReceived is called当应用程序处于前台并调用 onMessageReceived 时不显示通知
【发布时间】:2019-07-02 09:05:27
【问题描述】:

我正在尝试在收到 fcm 消息并且 APP 在后台时显示通知。我尝试了不同风格的通知,但没有运气。通知不会显示

这里是代码

public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(GlobalVar.TAG, "From: " + remoteMessage.getFrom());
        super.onMessageReceived(remoteMessage);


        String channelId = getString(R.string.notification_channel_id);
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0, builder.build());

    }

我知道这会被调用,因为我得到了日志。 如果应用程序在后台,则 FCM 会显示通知。当应用程序在前台时,上面的代码不显示它 有什么建议吗?

PS:我在Application类中创建了频道

我没有在日志猫中得到错误。我还按照下面的代码设置所有通知设置。请注意,我的设备在后台时已经接收并显示来自 FCM 的通知。但是当它在前台并且“我”处理通知的显示时,它就不起作用了

【问题讨论】:

  • 提供您的清单
  • 检查设置中是否允许该应用程序通知
  • 这也取决于你如何创建 NotificationChannel。尝试在您的代码中使用IMPORTANCE_HIGH 创建 NotificationChannel。在某些设备中,如果您的通知通道的优先级不高,则不会显示通知。
  • 在 FCM 的实施中。在您没有修改来自 FCM 的 JSON 之前,您无法在后台显示通知。您需要将 JSON 从通知标签修改为数据标签。
  • medium.com/@piyush052/…查看此链接。

标签: android firebase android-notifications firebase-notifications


【解决方案1】:

希望您的服务器通过data 键向您发送通知。

当我看到您的代码时,您正在使用 remoteMessage.getNotification()

获取通知数据

现在您必须在服务器端脚本中进行更改,他们在data 键中发送如下 (You can test temporary before changing server code)

{
 "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
 "collapse_key" : "type_a",
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title",
     "key_1" : "Value for key_1",
     "key_2" : "Value for key_2"
 }
}

然后你可以收到这样的:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }
}

要了解更多信息:

希望您能在这方面得到帮助。如果您有任何问题,请告诉我。

【讨论】:

  • 尽量不要在这里发布一些随机代码。我的意思是NotificationBean 是什么? #sendNotification() 在哪里?
  • @ADM 是的,让我改变一下。
  • 谢谢,但正如我在问题中所说的那样。 onMessageReceive 被调用,所以我知道我收到了来自 FCM 的电话。当应用程序在后台并通过 FCM 处理时,通知也已经工作。我的问题是,当应用程序处于前台并且“我”创建通知时,它不会显示
【解决方案2】:

如果您不使用自己的服务器,则无法实现此目的。您需要使用自己的服务器从这里修改 JSON 格式:

{
 "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title",
     "key_1" : "Value for key_1",
     "key_2" : "Value for key_2"
 }
}

到:

{
 "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
 "collapse_key" : "type_a",
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title",
     "key_1" : "Value for key_1",
     "key_2" : "Value for key_2"
 }
}

当您的服务器将使用“数据”修改“通知”标签时,只有您可以在后台接收通知。

【讨论】:

    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    相关资源
    最近更新 更多