【问题标题】:Why is my postman (data) message not reaching my android app?为什么我的邮递员(数据)消息没有到达我的 Android 应用程序?
【发布时间】:2019-07-22 10:18:09
【问题描述】:

previous question 中,我遇到了问题,即当应用程序在后台时,firebase 消息不会在我的应用程序的 firebase 类中触发 onMessageReceived() 事件。正如this friendly user 指出的那样,原因似乎是,firebase 控制台无法发送数据消息,它只发送不会到达事件的通知消息。他建议使用邮递员向我的应用程序发送数据消息。我向https://fcm.googleapis.com/fcm/send 设置了一个 POST 请求,并将我的项目的 API 密钥以及内容类型添加到 Headers 部分,如下所示:

Authorization = key=[firebase project key]
Content-Type = application/json

我在它的 firebase-token 的帮助下处理了我的应用程序:

{
    "to": "[token]",
    "notification": {
        "title": "This is a title"
        "body": "This is a test"
    },
    "data": {
        "title": "This is a data title"
        "body": "This is a data message"
        "key1": "value1"
    }
}

我向您展示的版本就像 firebase 控制台消息一样工作。它在前台(显示通知部分的内容)并传输数据时正确到达应用程序。但是在后台它会到达应用程序但不会触发 onMessageReceived() 事件。我试图删除通知部分以使消息成为“纯”数据消息,但在这种情况下,消息根本不会到达我的应用程序。 有人知道我做错了什么吗?

(我可以看出它没有到达 onMessageReceived 事件,因为在那里我为通知提供了不同的徽标以用于测试目的)

【问题讨论】:

    标签: java android firebase firebase-cloud-messaging postman


    【解决方案1】:

    显然这是因为您的通知部分为空,除非它不是,否则您只执行通知生成器。(至于来自previous question 的代码)

    if (remoteMessage.getNotification() != null ) {
            //if you send a data message only, this part here will never execute.
    }
    

    你可以把上面改成下面试试。

    if (remoteMessage.getNotification() != null || remoteMessage.getData() !=null ) {
           //This will execute when either notification or data message is not null
    }
    

    【讨论】:

    • 很高兴它有帮助。
    猜你喜欢
    • 2021-08-01
    • 2016-08-17
    • 2020-09-05
    • 2021-09-25
    • 2011-10-03
    • 2010-11-08
    • 2014-08-03
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多