0
votes

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

1

1 Answers

2
votes

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