简体   繁体   English

PubNub 推送通知在 Android 上发送不正确的数据

[英]PubNub Push Notification sends incorrect data on Android

Let me go straight to the point, with Firebase Cloud Messaging and Android Oreo there have been some major changes when it comes to using their APIs.让我直接切入正题,Firebase Cloud Messaging 和 Android Oreo 在使用它们的 API 时发生了一些重大变化。

I have entered my Firebase Server Api Key in the PubNub Console, push notification works absolutely fine on the Firebase console, but when publishing notification with PubNub, remoteMessage.toString gives => com.google.firebase.messaging.RemoteMessage@ffe9xxx in the OnMessageReceived function.我已经在 PubNub 控制台中输入了我的 Firebase 服务器 Api 密钥,推送通知在 Firebase 控制台上工作得非常好,但是当使用 PubNub 发布通知时,remoteMessage.toString 在 OnMessageReceived 中给出 => com.google.firebase.messaging.RemoteMessage@ffe9xxx功能。

I am publishing something like this我正在发布这样的东西

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);

in

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

Any idea why is this happening?知道为什么会这样吗? Thank you in advance.先感谢您。

Code on the receiving end接收端代码

There is a class which extends FirebaseMessagingService, codes for OnMessageReceived function:有一个扩展 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() );
 }

Android getData vs getNotification API Android getDatagetNotification API

You are nesting the notification key/value inside of the data key and just need to use the API, remoteMessage.getData() instead of remoteMessage.getNotification() .您将notification键/值嵌套在data键中,只需使用 API remoteMessage.getData()而不是remoteMessage.getNotification()

If notification key was at the top level, it would work.如果notification键在顶层,它会起作用。 See Android docs here . 请参阅此处的 Android 文档

Instead of this:取而代之的是:

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

This if switing to remoteMessage.getData() :如果切换到remoteMessage.getData()

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

Or this if sticking with remoteMessage.getNotification() :或者,如果坚持使用remoteMessage.getNotification()

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

PubNub basically just looks for the pn_gcm in the message payload when it is published and grabs whatever is inside of it and passes that directly to Google's FCM service for the devices that are registered (with PubNub) for that channel to receive GCM (FCM). PubNub 基本上只是在消息有效负载发布时查找pn_gcm并获取其中的任何内容,然后将其直接传递给 Google 的 FCM 服务,以便为该频道注册(使用 PubNub)的设备接收 GCM(FCM)。

If the data is not formatted properly we would receive an error back from FCM which should be reported on the channel's -pndebug channel (assuming pn_debug:true was included in the published message payload).如果数据格式不正确,我们将从 FCM 收到错误,该错误应该在通道的-pndebug通道上报告(假设pn_debug:true包含在已发布的消息有效负载中)。

For full details on troubleshooting FCM (GCM) or APONS issues with PubNub, please review How can I troubleshoot my push notification issues?有关对 PubNub 的 FCM (GCM) 或 APONS 问题进行故障排除的完整详细信息,请查看如何对我的推送通知问题进行故障排除?

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

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