Android FCM通知标题保留为“;FCM消息“;

Android FCM通知标题保留为“;FCM消息“;,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我正在尝试使用Firebase云消息传递。我将通知从Node.js服务器发送到注册到通知系统的应用程序 我的问题是,在Android 5.1上,即使我在NoFifification json中设置了title属性,通知也是“FCM Message”。它在安卓6.0中运行良好。我也尝试重新启动我的设备 这是我用来发送通知的代码: function sendNotificationToUser(userToken, message, onSuccess) { request({ url

我正在尝试使用Firebase云消息传递。我将通知从Node.js服务器发送到注册到通知系统的应用程序

我的问题是,在Android 5.1上,即使我在NoFifification json中设置了title属性,通知也是“FCM Message”。它在安卓6.0中运行良好。我也尝试重新启动我的设备

这是我用来发送通知的代码:

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();
    }
  });
}
正如您所看到的,我发送的通知标题是“我的应用程序名称”,但在设备上显示“FCM消息”


我该怎么办

我发现这是一个与
onMessageReceived
回调相关的问题


正如您在

上看到的,您需要传递标题,然后在
remoteMessage.getNotification().getTitle()
中接收它,这将捕获标题,然后显示在顶部,或者从web传递完整的JSON,然后像这样接收

JSONObject JSONObject=newjsonobject(remoteMessage.getData())

以下是完整的方法:

@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.
}
这是意料之中的。(已测试至Android 10)

FCM对应用程序状态有不同的行为(前台和后台/已终止)。 根据您的用例,您应该通过从服务器发送的有效负载来处理这个问题

从服务器发送的消息必须以“通知”或“数据”格式从仪表板或服务器端api发送。 注意:从firebase dashobard,您只能发送“通知”正文,而不能发送数据。在这种情况下,FCM将直接显示notif,而不回调您的应用程序

服务器端 以下是示例格式:

通知类型格式 注意:Android系统默认情况下会在通知托盘中显示通知,您无需显示

 { 
    "to": "your_token_id",
     "notification" : {
             "title" : "FCM Notification title!",
             "body" : "FCM Notification subtext!",
             "content_available" : true,
             "priority" : "high"
     }
}
数据格式(用于在应用程序中、前台和后台接收回调) 注意:您必须自己处理回调和显示notif

{ 
    "to": "your_token_id",

    "data" : {
         "title" : "FCM Notification Title ",
         "subtext" : "FCM Notification Sub Title",
         "type" : "999",
         "priority" : "high"
    }
}
Android客户端 要处理Android接收器中接收到的有效负载,请查看官方指南

检查文档


是由
onMessageReceived处理的通知还是在通知托盘中处理的通知?这不是服务器端问题,问题是Android code onMessageReceived
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.
}