0

I'm trying to Send Push notifications to multiple android devices.

For one device its working, but when I tried to add multiple device registrationIDs then its not; GCM returns Error=InvalidRegistration

var Message = tMessage.Text; //message text box   
            var Title = tTitle.Text;
            string stringregIds = null;
            List<string> regIDs = new List<string>();
            regIDs.Add(redIdEmulNew);
            regIDs.Add(regIdMobileNew);
             stringregIds = string.Join("\",\"", regIDs);   

            WebRequest tRequest;

            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");

            tRequest.Method = "post";

            tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";

            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
            string postData =
             "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
              + Message + "&data.title=" + Title + "&registration_id=" +
                 stringregIds + "";


            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);

            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();   //Get response from GCM server.

            lbResponse.Text = sResponseFromServer;      //Assigning GCM response to Label text 

            tReader.Close();

            dataStream.Close();
            tResponse.Close();

I suspect below code has issue:

string stringregIds = null;
List<string> regIDs = new List<string>();
regIDs.Add(redIdEmulNew);
regIDs.Add(regIdMobileNew);
stringregIds = string.Join("\",\"", regIDs); 

Both device registration Ids are valid, I have checked push notifications individually.

Thanks

1 Answer 1

1

After searching for a long time I found "Multicast messages (sending to more than 1 registration IDs) are allowed using HTTP JSON format only"

Here is the reference https://developers.google.com/cloud-messaging/server-ref#table1

and an example http://labs.distriqt.com/post/1223

string postData =
"{ \"registration_ids\": [ \"" + stringregIds + "\" ], " +
"\"data\": {\"title\":\"" + Title + "\", " +
"\"message\": \"" + Message + "\"}}";
Sign up to request clarification or add additional context in comments.

1 Comment

tRequest.ContentType = " application/json"; - Set the correct format to go with the changes suggested by Ashok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.