Android通知使用GCM和Java使用json消息推空 [英] Android notification push empty using GCM and java with json message

查看:82
本文介绍了Android通知使用GCM和Java使用json消息推空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我写的一小段代码:

 字符串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();

响应响应= 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 ,它在我尝试发送至-sync选项。我在手机上收到通知声音,但没有实际通知,因为没有任何消息或数据链接到推送。



现在,当我将数据字符串使用以下代码行,我仍然得到相同的结果:

  String data ={\notification \:{ \标题\:\测试标题\,\文字\:\这是一个测试\},\to \:\ 
+ to +\};

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



他们写入日志看起来像这样:


Content-Type:application / json; charset = UTF-8

日期:2016年4月21日星期四10:51:23 GMT
$ b 过期时间:周四,2016年4月21日10:51:23 GMT



Cache-Control:private,max-age = 0

< X-Content-Type-Options:nosniff



X-Frame-Options:SAMEORIGIN



XSS保护:1; mode = block



服务器:GSE



替代协议:443:quic

Alt-Svc:quic =:443; MA = 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}]}


解决方案

日志条目表明您的邮件已成功处理。但是,对于带有载荷选项的通知,您应该发送如下所示的JSON消息:

  {
to: < your-recipient-id>,
notification:{
body:这是一个测试,
title:我的测试




$ b $ p
$ b

请按建议更改,并告诉我们是否有帮助。
您可以在开发者指南中查看消息概念和选项 - 该指南提供了一些有用的示例。


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

  public void onMessageReceived(String from,Bundle data){
String notificationJSONString = data.getString(通知);
//然后你可以将notificationJSONString解析为一个JSON对象
JSONObject notificationJSON = new JSONObject(notificationJSONString);
String body = notificationJSON.getString(body);
Log.d(TAG,Notification Message is:+ body);
}


I'm struggling to push notifications to Android devices.

Here is a small piece of code I wrote:

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

Looking at https://developers.google.com/cloud-messaging/http#send-to-sync, it works fine when I try the send-to-sync option. I get a notification sound on my phone, but there is no actual notification, since there is no message or data linked to the push.

Now when I replace my data String with the following line, I still get the same result:

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

I'm not sure what I'm missing. My guess is that my format of the notification is wrong, but I've tried every combination that I've found so far during my research.

They writes to the log looks like this:

Content-Type: application/json; charset=UTF-8

Date: Thu, 21 Apr 2016 10:51:23 GMT

Expires: Thu, 21 Apr 2016 10:51:23 GMT

Cache-Control: private, max-age=0

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-XSS-Protection: 1; mode=block

Server: GSE

Alternate-Protocol: 443:quic

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

Transfer-Encoding: chunked

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"}]}

解决方案

The log entry indicates that your message was processed successfully. However for the notification with payload option you should send a JSON message like this:

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

Please change as suggested and let us know if this helps. You can check the Messaging Concepts and Options on Developer Guides - the guide provides some useful examples.

Maybe as an example - this is how you can handle the notifications. In your app's onMessageReceived() you process it by retrieving the notification payload using notification as a key. For example:

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

这篇关于Android通知使用GCM和Java使用json消息推空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆