2015-08-17 201 views
0

我試圖發送推送通知到多個android設備。發送推送通知到多個Android設備使用asp .net

對於一個設備它的工作,但是當我試圖添加多個設備 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

搜索時間長,我發現「多播消息(發送至多於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 + "\"}}"; 
+0

tRequest.ContentType =「應用/ JSON「; - 設置正確的格式以符合Ashok建議的更改。 –

相關問題