【发布时间】: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