Send Multi Notification to GCM using JSON
The JSON should look like this :
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",...],
"data" : {
"Team" : "Portugal",
"Score" : "3",
"Player" : "Varela",
},
}
You forgot to create a dictionary for the data.
From GCM documentation :
data
A JSON object whose fields represents the key-value pairs of the message's payload data. If present, the payload data it will be included in the Intent as application data, with the key being the extra's name. For instance, "data":{"score":"3x1"} would result in an intent extra named score whose value is the string 3x1. There is no limit on the number of key/value pairs, though there is a limit on the total size of the message (4kb). The values could be any JSON object, but we recommend using strings, since 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 conversion to string yourself. Also note that the key cannot be a reserved word (from or any word starting with google.). To complicate things slightly, there are some reserved words (such as collapse_key) that are technically allowed in payload data. However, if the request also contains the word, the value in the request will overwrite the value in the payload data. Hence using words that are defined as field names in this table is not recommended, even in cases where they are technically allowed. Optional.
Related videos on Youtube
Husam A. Al-ahmadi
Updated on June 04, 2022Comments
-
Husam A. Al-ahmadi almost 2 years
I want to send multiple notifications to many registered devices at once using GCM, and I know that that can not be done using the plain text since it only can be used to send notification for one device at time. So instead I decided to make the HTTP body content using JSON and I managed to make the following string that represents the JSON object:
{ collapse_key : my_CollapseKey, data.message : myMessage, registration_ids : ["regid1","regid2",...] }
But whenever I send the request I get a 400 response which states Bad Request, and when I refer to the document of GCM Architectural Overview it states that
Response 400 Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields (for instance, passing a string where a number was expected)
Here is the snapshot from my web app developed in ASP.Net 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 I can parse my JSON object correctly and send multiple notifications? any help will be completely appreciated.
regards
-
Husam A. Al-ahmadi about 11 yearsIS IT registration_id or registration_ids since I want to make multi sending here
-
Eran about 11 yearsSorry about that, fixed it to registration_ids.
-
Husam A. Al-ahmadi about 11 yearsI tried it the way you suggested but did not work, and I really did not got your point in creating a dictionary for data
-
Eran about 11 yearsI mean that instead of putting
data.message : myMessage
in your JSON, you should put"data" : {"message" : "myMessage"}
. That would make the value of the data key a dictionary. That's how the GCM documentation says it should be - developer.android.com/google/gcm/gcm.html. -
Husam A. Al-ahmadi about 11 years{registration_ids:["APA91bGzn_0QP_hWt7f-whQKkAJ4-iuGlRfQqA_sWcjvOZZ3v-5y5PSNFEPrD-siR5WSyskTVaBST5LHDsIFCTZqY_-ectSPDBUeJGstmabKSLkcoNuJb5CngeQ__3NCwRdrXF3WKkirMbE-TdHABDyRFGgJEJK-dQ",".....","...."],data:{message:Wlecome to the app,},collapse_key:wlecome_message,} this my json object and in response I got nothing like word nothing
-
Eran about 11 yearsYou should use quotes for all the Strings within the JSON, so it should be :
{"registration_ids":["APA91bGzn_0QP_hWt7f-whQKkAJ4-iuGlRfQqA_sWcjvOZZ3v-5y5PSNFEPrD-siR5WSyskTVaBST5LHDsIFCTZqY_-ectSPDBUeJGstmabKSLkcoNuJb5CngeQ__3NCwRdrXF3WKkirMbE-TdHABDyRFGgJEJK-dQ",".....","...."],"data":{"message":"Wlecome to the app",},"collapse_key":"wlecome_message",}
-
Husam A. Al-ahmadi about 11 yearsThanks alot Eran for your contributions which without it I will be stoke in this for ever, I really did not think that I need to quote string. Really thanks