【问题标题】:PubNub Push Notification sends incorrect data on AndroidPubNub 推送通知在 Android 上发送不正确的数据
【发布时间】:2018-02-21 04:42:59
【问题描述】:

让我直奔主题,Firebase Cloud Messaging 和 Android Oreo 在使用它们的 API 时发生了一些重大变化。

我已经在 PubNub 控制台中输入了我的 Firebase 服务器 Api 密钥,推送通知在 Firebase 控制台上工作得非常好,但是当使用 PubNub 发布通知时,remoteMessage.toString 在 OnMessageReceived 函数中给出 => com.google.firebase.messaging.RemoteMessage@ffe9xxx

我正在发布类似的内容

JsonObject payload = new JsonObject();

        JsonObject androidData = new JsonObject();
        androidData.addProperty("contentText","test content");
        androidData.addProperty("contentTitle","Title");

        JsonObject notification = new JsonObject();
        notification.add("notification",androidData);


        JsonObject data = new JsonObject();
        data.add("data", notification);
        payload.add("pn_gcm", data);

PubNubObject.publish()
            .message(payload)
             etc..

知道为什么会这样吗? 提前谢谢你。

接收端的代码

有一个扩展 FirebaseMessagingService 的类,代码为OnMessageReceived 函数:

if (remoteMessage.getNotification() != null) {
    //for testing firebase notification
    Log.d(TAG, "Message Notification 
    Body:"+remoteMessage.getNotification().getBody());  
 } else {
    //for anything else, I wanted to see what was coming from the server
    //this is where I am getting the message when using PubNub notification
    Log.d(TAG, "onMessageReceived: remoteMessage to 
    str:"+remoteMessage.toString() );
 }

【问题讨论】:

  • 你的代码在消息接收端是什么样子的?
  • @CraigConover 感谢您的回复,我已经用您要求的代码更新了帖子
  • 我相信正在发生的事情是您将 notification 键/值嵌入到 data 键中,您可能只需要使用 API,remoteMessage.getData() 而不是 remoteMessage.getNotification() .如果notification 键位于顶层,它可能会起作用。见Android docs here。请让我知道这是否有效。
  • 我相信您直接联系了我们的支持。如果是这样,我们解决的任何问题都将作为答案发回此处。
  • 问题确实出在remoteMessage.getNotification(),应该是remoteMessage.getData()。感谢您宝贵的时间。

标签: android firebase firebase-cloud-messaging pubnub


【解决方案1】:

Android getDatagetNotification API

您将notification 键/值嵌套在data 键中,只需要使用API​​,remoteMessage.getData() 而不是remoteMessage.getNotification()

如果notification 键位于顶层,它将起作用。 See Android docs here.

而不是这个:

{
 "pn_gcm": {
  "data": {
   "notification": {
    "contentText": "test content",
    "contentTitle": "Title"
   }
  }
 }
}

如果切换到remoteMessage.getData()

{
 "pn_gcm": {
  "data": {
    "contentText": "test content",
    "contentTitle": "Title"
  }
 }
}

或者如果坚持使用remoteMessage.getNotification()

{
 "pn_gcm": {
  "notification": {
    "contentText": "test content",
    "contentTitle": "Title"
   }
  }
 }
}

PubNub 基本上只是在消息负载发布时在消息负载中查找pn_gcm,并抓取其中的任何内容并将其直接传递给 Google 的 FCM 服务,以便为该通道注册(使用 PubNub)的设备接收GCM (FCM)。

如果数据格式不正确,我们会从 FCM 收到错误消息,该错误消息应在通道的 -pndebug 通道上报告(假设 pn_debug:true 包含在已发布的消息负载中)。

有关对 PubNub 的 FCM (GCM) 或 APONS 问题进行故障排除的完整详细信息,请查看 How can I troubleshoot my push notification issues?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    相关资源
    最近更新 更多