Send Multi Notification to GCM using JSON

I want to send multiple notifications to many registered devices at the same time using GCM and I know this cannot be done using plain text as it can only be used to send notifications to one device only. So instead, I decided to create the content of the HTTP body using JSON, and I was able to create the following line that represents the JSON object:

{ collapse_key : my_CollapseKey, data.message : myMessage,  registration_ids : ["regid1","regid2",...] }

      

But whenever I submit a request, I get a 400 response that says Bad Request, and when I refer to the GCM Architectural Overview document it is that

Reply 400 Applicable only for JSON requests. Indicates that the request could not be parsed as JSON or contains invalid fields (for example, passing a string where a number was expected)

Here is a snapshot of my ASP.Net web application that I used to send the request:

request.ContentType = "application/json"
        request.Headers.Add("Authorization: key=My_Server_Key")
        request.Headers.Add("Sender: id=myProject_ID")
        Dim collapsKey = Guid.NewGuid.ToString("n")
        Dim data As String = "{ collapse_key : " + collapsKey + "," + " data.message : " + HttpUtility.UrlEncode(TextBox1.Text) + "}" + ", registration_ids : " + jsonids1 + "}"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(data)
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

      

Any idea how to parse the JSON object correctly and send multiple notifications? any help would be fully appreciated.

considers

+3


source to share


1 answer


JSON should look like this:

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",...],
  "data" : {
    "Team" : "Portugal",
    "Score" : "3",
    "Player" : "Varela",
  },
}

      

You forgot to create a data dictionary.



From GCM documentation:

data
A JSON object whose fields represent the key-value pairs of the message payload data. If present, the payload data will be included in the intent as application data, with the key being an alternate name. For example, "data": {"score": "3x1"} will result in an extra extra score, the value of which is a 3x1 row. There is no limit on the number of key / value pairs, although there is a limit on the total message size (4kb). The values ​​can be any JSON objects, but we recommend using strings as the values ​​will be converted to strings in the GCM server anyway. If you want to include objects or other non-string data types (such as integers or booleans), you have to do the string conversion yourself.Also note that the key cannot be a reserved word (from or any word starting with google.). To complicate things a bit, there are some reserved words (like collapse_key) that are technically allowed in the payload data. However, if the request also contains a word, the value in the request will overwrite the value in the payload data. Therefore, using words that are defined as field names in this table is not recommended, even in cases where they are technically permitted. Not necessary.the value in the request will overwrite the value in the payload data. Therefore, using words that are defined as field names in this table is not recommended, even in cases where they are technically permitted. Not necessary.the value in the request will overwrite the value in the payload data. Therefore, using words that are defined as field names in this table is not recommended, even in cases where they are technically permitted. Not necessary.

+6


source







All Articles