简体   繁体   English

FCM 通知标题仍为“FCM 消息”

[英]FCM notification title remains "FCM Message"

I'm trying to use the Firebase Cloud Messaging.我正在尝试使用 Firebase 云消息传递。 I send the notifications from a Node.js server to my apps that are registered to the notification system.我将通知从 Node.js 服务器发送到我注册到通知系统的应用程序。

My problem is that on Android 5.1 the notification is "FCM Message" even if I setted the title attribute in the nofitification json.我的问题是在 Android 5.1 上,即使我在通知 json 中设置了标题属性,通知也是“FCM 消息”。 It works fine in Android 6.0.它在 Android 6.0 中运行良好。 I tried to reboot my device as well.我也尝试重新启动我的设备。

在此处输入图像描述

And this is the code I use to send the notification:这是我用来发送通知的代码:

function sendNotificationToUser(userToken, message, onSuccess) {
  request({
    url: 'https://fcm.googleapis.com/fcm/send',
    method: 'POST',
    headers: {
      'Content-Type' :' application/json',
      'Authorization': 'key='+API_KEY
    },
    body: JSON.stringify({
      notification: {
        "title": 'My App Name',
        "body": message,
        "sound": 'default'
      },
      to : userToken
    })
  }, function(error, response, body) {
    if (error) { console.error(error); }
    else if (response.statusCode >= 400) { 
      console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    }
    else {
      onSuccess();
    }
  });
}

As you can see the notification title I send is "My App Name" but on device it shows "FCM Message".如您所见,我发送的通知标题是“我的应用名称”,但在设备上显示“FCM 消息”。

What do I have to do?!我需要做什么?!

You need to pass the title and then receive it in remoteMessage.getNotification().getTitle() , this will catch title and then display in the top or pass complete JSON from web and receive like this您需要传递标题,然后在remoteMessage.getNotification().getTitle()接收它,这将捕获标题然后显示在顶部或从网络传递完整的 JSON 并像这样接收

JSONObject jsonObject = new JSONObject(remoteMessage.getData());

Here is the complete method:这是完整的方法:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://firebase.google.com/support/faq/#fcm-android-background
    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());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Ref link 参考链接

I found that it is a problem related to onMessageReceived callback.我发现这是一个与onMessageReceived回调相关的问题。

在此处输入图片说明

As you can see on the receive a FCM guide正如您在接收 FCM 指南中看到的那样

This is expected.这是预期的。 (Tested upto Android 10) (测试到 Android 10)

FCM has different behaviours for app status (foreground and background / killed). FCM 对应用程序状态(前台和后台/已终止)有不同的行为。 You should handle this by the payload you sent from server, according to your use case.根据您的用例,您应该通过从服务器发送的有效负载来处理此问题。

The msg sent from server has to be sent in either "notification" or "data" format, from dashboard or server side api.从服务器发送的 msg 必须以“通知”或“数据”格式从仪表板或服务器端 api 发送。 Note: From firebase dashobard you can only send "notification" body and not data.注意:从 firebase dashobard 您只能发送“通知”正文而不是数据。 In such cases, FCM will directly display the notif without giving a callback to your app.在这种情况下,FCM 将直接显示通知而不会给您的应用程序回调。

Server side Below are sample formats :服务器端以下是示例格式:

Notification Type Format Note : Android System will by default display the notification in the notification tray and you don't need to display it.通知类型格式 注意:Android 系统默认会在通知托盘中显示通知,您不需要显示它。

 { 
    "to": "your_token_id",
     "notification" : {
             "title" : "FCM Notification title!",
             "body" : "FCM Notification subtext!",
             "content_available" : true,
             "priority" : "high"
     }
}

Data Format (For receiving callback in app, in foreground and background) Note : You have to handle callback and display notif on your own.数据格式(用于在应用程序中接收回调,在前台和后台) 注意:您必须自己处理回调和显示通知。

{ 
    "to": "your_token_id",

    "data" : {
         "title" : "FCM Notification Title ",
         "subtext" : "FCM Notification Sub Title",
         "type" : "999",
         "priority" : "high"
    }
}

Android Client To handle the payload received in your Android receiver, checl the official guide here Android 客户端要处理在您的 Android 接收器中接收到的有效负载,请在此处查看官方指南

override fun onMessageReceived(remoteMessage: RemoteMessage) {

    Log.d(TAG, "From: ${remoteMessage.from}")

    // Check if message contains a data payload.
    remoteMessage.data.isNotEmpty().let {
        Log.d(TAG, "Message data payload: " + remoteMessage.data)

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }

    // Check if message contains a notification payload.
    remoteMessage.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Check the documentation here检查文档here

在此处输入图片说明

This is how i get the title from remote messages:这就是我从远程消息中获取标题的方式:

 var title = ""
 override fun onMessageReceived(remoteMessage: RemoteMessage) {
    if (remoteMessage.notification != null) {
        title = remoteMessage.notification!!.title!!
    }
}


val notificationBuilder = NotificationCompat.Builder(applicationContext, channelId)
                         .setContentTitle(title)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM