2016-05-14 15 views
0

我有以下代碼給出GCM服務器的「錯誤:MissingRegistration」響應。通過張貼郵差或提琴手「POSTDATA」返回錯誤:使用asp.net在GCM中丟失註冊

public void SendPushNotification() 
    { 

     string stringregIds = null; 
     List<string> regIDs = _db.Directories.Where(i => i.GCM != null && i.GCM != "").Select(i => i.GCM).ToList(); 
     //Here I add the registrationID that I used in Method #1 to regIDs 
     stringregIds = string.Join("\",\"", regIDs); 
     //To Join the values (if ever there are more than 1) with quotes and commas for the Json format below 

     try 
     { 
      string GoogleAppID = "AIzaSyA2Wkdnp__rBokCmyloMFfENchJQb59tX8"; 
      var SENDER_ID = "925760665756"; 
      var value = "Hello"; 
      WebRequest tRequest; 
      tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); 
      tRequest.Method = "post"; 
      Request.ContentType = "application/json"; 
      tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID)); 
      tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID)); 

      string postData = "{\"collapse_key\":\"score_update\",\"time_to_live\":108,\"delay_while_idle\":true,\"data\": { \"message\" : " + "\"" + value + "\",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\"},\"registration_ids\":[\"" + 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(); 
      TempData["msg1"] = "<script>alert('" + sResponseFromServer + "');</script>"; 
      HttpWebResponse httpResponse = (HttpWebResponse)tResponse; 
      string statusCode = httpResponse.StatusCode.ToString(); 

      tReader.Close(); 
      dataStream.Close(); 
      tResponse.Close(); 
     } 
     catch (Exception ex) 
     { 
     } 
    } 

但確切字符串給出肯定的答覆,並通知到達設備。

我在代碼中缺少的東西請幫助我。

postData返回該值已成功發佈由郵遞員,提琴手

{"collapse_key":"score_update","time_to_live":108,"delay_while_idle":true,"data": { "message" : "Hello","time": "5/13/2016 5:50:59 PM"},"registration_ids":["cXMf6hkYIrw:APA91bGr-8y2Gy-qzNJ3zjrlf8t-4m9uDib9P0j8GW87bH5jq891-x_7P0qqItzlc_HXh11Arg76lCOcjXPrU9LAgtYLwllH2ySxA0ADSfiz3qPolajjvI3d3zE6Rh77dwRqXn3NnbAm"]} 

回答

1

在你的代碼的問題是在ContentType

代替使用這個: -

Request.ContentType = "application/json"; 

使用

tRequest.ContentType = "application/json"; 

它將工作

+0

多麼愚蠢的錯誤。感謝哥們。 –

+0

還有一個問題,如果我有超過100個設備? –

+0

@صفي只是通過您的stringregIds變量中的所有註冊ID它將工作 –

0

試試這個: -

private string SendGCMNotification() 
    { 
     try 
     { 
      string message = "some test message"; 
      //string tickerText = "example test GCM"; 
      string contentTitle = "content title GCM"; 
      string registration_id = "cXMf6hkYIrw:APA91bGr-8y2Gy-qzNJ3zjrlf8t-4m9uDib9P0j8GW87bH5jq891-x_7P0qqItzlc_HX‌​h11Arg76lCOcjXPrU9LAgtYLwllH2ySxA0ADSfiz3qPolajjvI3d3zE6Rh77dwRqXn3NnbAm"; 
      string postData = 
      "{ \"registration_ids\": [ \"" + registration_id + "\" ], " + 
       "\"data\": {\"contentTitle\":\"" + contentTitle + "\", " + 
         "\"message\": \"" + message + "\"}}"; 

      string GoogleAppID = "AIzaSyA2Wkdnp__rBokCmyloMFfENchJQb59tX8"; 

      WebRequest wRequest; 
      wRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); 
      wRequest.Method = "POST"; 
      wRequest.ContentType = " application/json;charset=UTF-8"; 
      wRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID)); 

      var bytes = Encoding.UTF8.GetBytes(postData); 
      wRequest.ContentLength = bytes.Length; 

      var stream = wRequest.GetRequestStream(); 
      stream.Write(bytes, 0, bytes.Length); 
      stream.Close(); 

      var wResponse = wRequest.GetResponse(); 
      stream = wResponse.GetResponseStream(); 
      var reader = new StreamReader(stream); 
      var response = reader.ReadToEnd(); 

      var httpResponse = (HttpWebResponse)wResponse; 
      var status = httpResponse.StatusCode.ToString(); 

      reader.Close(); 
      stream.Close(); 
      wResponse.Close(); 

      //TODO check status 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     return "sent"; 
    } 
+0

@RÇ感謝名單,但仍然得到同樣的錯誤。 –

+0

你能告訴我stringregIds變量中的數據嗎?我認爲在通過註冊ID時存在問題 –

+0

我編輯了我的問題。請看最後一行。 –