google Cloud Messaging Push notification

2019-02-04 12:20发布

问题:

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.

回答1:

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



回答2:

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...\" }"


回答3:

Yes, you can use POSTMAN.

This GCM Notification Test Tool simplifies the server side testing immensely by reducing the number of items you enter in POSTMAN everytime - http://techzog.com/development/gcm-notification-test-tool-android/