【问题标题】:Android notification push empty using GCM and java with json message使用 GCM 和带有 json 消息的 java 的 Android 通知推送为空
【发布时间】:2016-08-14 13:22:18
【问题描述】:

我很难将通知推送到 Android 设备。

这是我写的一小段代码:

    String API_KEY = "AIzaSy....";
    String url = "https://android.googleapis.com/gcm/send";
    String to = "ce0kUrW...";

    String data = "{\"to\": \"" + to + "\"}";

    RequestBody body = RequestBody.create(MediaType.parse("application/json"), data);

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).post(body).addHeader("Authorization", "key=" + API_KEY).build();

    Response response = client.newCall(request).execute();
    System.out.println(response.headers());
    System.out.println(response.body().string());

查看https://developers.google.com/cloud-messaging/http#send-to-sync,当我尝试发送到同步选项时它工作正常。我在手机上听到通知声音,但没有实际通知,因为没有消息或数据链接到推送。

现在当我用以下行替换我的数据字符串时,我仍然得到相同的结果:

    String data = "{ \"notification\": {\"title\": \"Test title\", \"text\": \"Hi, this is a test\"},\"to\" : \""
    + to + "\"}";

我不确定我错过了什么。我的猜测是我的通知格式是错误的,但我已经尝试了迄今为止在研究过程中发现的所有组合。

他们写入日志如下所示:

内容类型:应用程序/json; charset=UTF-8

日期:2016 年 4 月 21 日星期四 10:51:23 GMT

到期:2016 年 4 月 21 日星期四 10:51:23 GMT

缓存控制:私有,max-age=0

X-Content-Type-Options: nosniff

X-Frame-选项:SAMEORIGIN

X-XSS-保护:1;模式=块

服务器:GSE

替代协议:443:quic

Alt-Svc: quic=":443";马=2592000; v="32,31,30,29,28,27,26,25"

传输编码:分块

OkHttp-Selected-Protocol: http/1.1

OkHttp-Sent-Millis:1461235877551

OkHttp-Received-Millis:1461235877855

{"multicast_id":6596853786874127657,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1461235883968556%6ff215a7f9fd7ecd"}]}

【问题讨论】:

    标签: java google-cloud-messaging


    【解决方案1】:

    日志条目表明您的消息已成功处理。但是对于带有有效负载选项的通知,您应该发送这样的 JSON 消息:

    {
        "to" : "<your-recipient-id>",
        "notification" : {
          "body" : "Hi, this is a test",
          "title" : "My test"
        }
      }
    

    请按照建议进行更改,如果这有帮助,请告诉我们。 您可以查看开发人员指南上的Messaging Concepts and Options - 该指南提供了一些有用的示例。

    也许作为一个例子 - 这就是您可以处理通知的方式。 在您的应用程序的onMessageReceived() 中,您通过使用notification 作为键检索通知有效负载来处理它。例如:

    public void onMessageReceived(String from, Bundle data) {
         String notificationJSONString = data.getString("notification");
         //then you can parse the notificationJSONString into a JSON object
         JSONObject notificationJSON = new JSONObject(notificationJSONString ); 
         String body = notificationJSON.getString("body");
         Log.d(TAG, "Notification Message is : " + body);
      }
    

    【讨论】:

    • 使用“body”做同样的事情。我也尝试过该页面上的示例(在我研究的早期),但我没有得到任何快乐。
    • 查看更新的答案 - 我们首先确认您是否在 onMessageReceived 函数中收到消息/通知。
    • 你可能刚刚为我打开了另一罐蠕虫病毒。所以我的例子是服务器端,消息被构建和发送。我是否正确理解然后有一个客户端(GcmListenerService 使用“onMessageReceived”方法坐在那里?这是处理消息并将其传递到手机的地方?我认为我无法访问客户端。
    • 是的!查看本教程,了解如何try Cloud Messaging for Android
    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多