Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google Cloud Messaging Push notification

Can I use POSTMAN client on Google Chrome to send payload message to GCM server for testing purpose. Secondly if yes, what is the header and url parameter to be sent.

like image 669
souravlahoti Avatar asked Jan 10 '15 08:01

souravlahoti


2 Answers

Yes, you can.

1. Send a notification with a JSON payload

URL: https://android.googleapis.com/gcm/send

Headers:

  • Authorization: key=<your-api-key>
  • Content-Type: application/json

Body (click on the 'raw' tab):

{
  "collapse_key": "score_update",
  "time_to_live": 108,
  "delay_while_idle": true,
  "data": {
    "score": "4x8",
    "time": "15:16.2342"
  },
  "registration_ids":["4", "8", "15", "16", "23", "42"]
}

Note: registration_ids is the only required field, all the others are optional.

2. Send a notification with a plain text payload

URL: https://android.googleapis.com/gcm/send

Headers:

  • Authorization: key=<your-api-key>
  • Content-Type: application/x-www-form-urlencoded;charset=UTF-8

Body (click on the 'x-www-form-urlencoded' tab):

collapse_key=score_update
time_to_live=108
delay_while_idle=1
data.score=4x8
data.time=15:16.2342
registration_id=42

Note: registration_id is the only required field, all the others are optional.


Source: https://developer.android.com/google/gcm/http.html

like image 139
aluxian Avatar answered Oct 03 '22 21:10

aluxian


Just for the record and to complete the nice answer from @Alexandru Rosianu the GCM endpoint changed a while ago and it is suggested to use the new one. Here is an example taken from the official docs:

Authentication

To send a message, the application server issues a POST request. For example:

https://gcm-http.googleapis.com/gcm/send

A message request is made of 2 parts: HTTP header and HTTP body.

The HTTP header must contain the following headers:

  • Authorization: key=YOUR_API_KEY
  • Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text. If Content-Type is omitted, the format is assumed to be plain text.

For example:

Content-Type:application/json
Authorization:key=YOUR_API_KEY

{
  "notification": {
      "title": "Portugal vs. Denmark",
      "text": "5 to 1"
  },
  "to" : "bk3RNwTe3H0:CI2k_H..."
}

The HTTP body content depends on whether you're using JSON or plain text. See the Server Reference for a list of all the parameters your JSON or plain text message can contain.

Example using Curl:

# curl --header "Authorization: key=YOUR_API_KEY" \
       --header Content-Type:"application/json" \
       https://gcm-http.googleapis.com/gcm/send \
       -d "{\"notification\": { \"title\": \"Portugal vs. Denmark\"," \
          "\"text\": \"5 to 1\" }, \"to\" : \"bk3RNwTe3H0:CI2k_H...\" }"
like image 45
GoRoS Avatar answered Oct 03 '22 21:10

GoRoS