【问题标题】:FCM notification title remains "FCM Message"FCM 通知标题仍为“FCM 消息”
【发布时间】:2016-11-02 15:08:00
【问题描述】:

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

我的问题是,在 Android 5.1 上,即使我在 nofitification json 中设置了 title 属性,通知也是“FCM 消息”。它在 Android 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处理的通知还是通知托盘中的通知?
  • 这不是服务器端问题,问题是 Android 代码 onMessageReceived

标签: android firebase firebase-cloud-messaging


【解决方案1】:

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

JSONObject jsonObject = new JSONObject(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.
}

Ref link

【讨论】:

    【解决方案2】:

    发现是onMessageReceived回调相关的问题。

    正如您在receive a FCM guide 上看到的那样

    【讨论】:

      【解决方案3】:

      这是意料之中的。 (测试到 Android 10)

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

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

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

      通知类型格式 注意:Android系统默认会在通知托盘中显示通知,您不需要显示。

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

      数据格式(用于在应用中接收回调,在前台和后台) 注意:你必须自己处理回调和显示通知。

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

      Android 客户端 要处理在您的 Android 接收器中收到的有效负载,请查看官方指南 here

      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.
      }
      

      查看文档here

      【讨论】:

        猜你喜欢
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2017-02-23
        • 2017-03-16
        • 1970-01-01
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多