【问题标题】:Send Firebase API Notification with "extra information" using JSONObject in android在android中使用JSONObject发送带有“额外信息”的Firebase API通知
【发布时间】:2019-04-03 00:31:16
【问题描述】:

我正在开发一个 android 应用程序,我正在使用 Firebase API 在 android studio 中使用 HTTP POST 发送推送通知。我按照教程here 发送标题和正文。它工作正常。我想发送额外的信息,请建议我如何实现?这是我的代码:

            ...

            JSONObject json = new JSONObject();                                                                     
            json.put("to", "/topics/" + "Greetings");

            JSONObject info = new JSONObject();
            info.put("title", "Hi");
            info.put("body", "Good morning");
            json.put("notification",info);

            JSONObject data = new JSONObject();
            data.put("extra_information", PreferenceUtils.getUserID());
            json.put("extra_information", data);

            ...

【问题讨论】:

    标签: android json firebase firebase-cloud-messaging http-post


    【解决方案1】:

    Firebase json 有效负载如下所示:

    [
        "to" => 'DEVICE_TOKEN',
        "notification" => [
            "body" => "SOMETHING",
            "title" => "SOMETHING",
            "icon" => "ic_launcher"
        ],
        "data" => [
            "ANYTHING EXTRA HERE"
        ]
    ]
    

    使用以下内容获取额外信息。

    JSONObject extras = new JSONObject();
    extras.put("message", "Happy birthday!");
    extras.put("userId", PreferenceUtils.getUserID());
    json.put("data",extras);
    

    【讨论】:

    • 感谢您的回复@farhanjk。我试过了,但是没有收到额外的信息。
    • 你如何检查extra信息是否收到?
    • 谢谢!它现在工作正常。很抱歉回复晚了@farhanjk。问题是我如何在 FirebaseMessagingService 中收到额外的。我收到的是remoteMessage.getNotification() 而不是remoteMessage.getData()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2016-09-26
    • 2023-03-27
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多