使用asp .net

时间:2015-08-17 05:55:49

标签: android asp.net google-cloud-messaging

我正在尝试向多个Android设备发送推送通知。

  

对于其中一台设备正常工作,但当我尝试添加多台设备时   registrationIDs则不然; GCM返回错误= 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();

我怀疑下面的代码有问题:

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

两个设备注册ID都有效,我已单独检查推送通知。

由于

1 个答案:

答案 0 :(得分:1)

搜索了很长时间后,我发现“多播消息(发送到多个注册ID)仅允许使用HTTP JSON格式

以下是参考https://developers.google.com/cloud-messaging/server-ref#table1

和示例http://labs.distriqt.com/post/1223

string postData =
"{ \"registration_ids\": [ \"" + stringregIds + "\" ], " +
"\"data\": {\"title\":\"" + Title + "\", " +
"\"message\": \"" + Message + "\"}}";