2016-12-02 253 views
0

我想通過FCM向C#發送推送通知給Android。使用FCM向Android推送通知(c#)

我已經使用了下面的代碼,它給了我在響應中的id,似乎一切都好。

但我沒有收到任何通知在我的設備中,因爲設備ID是正確的,並收到通知,如果我通過FCM控制檯發送它。

任何建議都會有所幫助。

感謝

var value = "This is first message to Android"; 
      WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 
      tRequest.Method = "post"; 
      tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; 
      tRequest.Headers.Add(string.Format("Authorization: key={0}", "AAAAosN-NoU:APA91bHg6Iv8qj2giVvSRVVFze4L6Si0VTwctkNaRRqlI7qffKAMPHo4fd52SgZcB8Ring-mnPrkqjodTGrSru_bXKRQD9n4eVxhs8pTbkXac1gotCM77EixMUXTplXhceIm1o20BVXvkHO1HQZBti990ijebUW1fg")); 
      tRequest.Headers.Add(string.Format("Sender: id={0}", "699064530565")); 

      string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + "APA91bFD6jRdsSzQ37_rB2x4C8wJzcDzgdjZuhf4hFgzCndeESCEnHrW3sRQcwetljExhlEUrpquhdTW2C24qjtb-PJuUUhpnev3Zn_DDH6aF7PStFRPYvWSQcfLFd14sueLmbhCvcqr" + ""; 

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

      using (Stream dataStream = tRequest.GetRequestStream()) 
      { 
       dataStream.Write(byteArray, 0, byteArray.Length); 

       using (WebResponse tResponse = tRequest.GetResponse()) 
       { 
        using (Stream dataStreamResponse = tResponse.GetResponseStream()) 
        { 
         using (StreamReader tReader = new StreamReader(dataStreamResponse)) 
         { 
          string sResponseFromServer = tReader.ReadToEnd(); 
         } 
        } 
       } 
      } 
+0

沒有人知道....離開它我會解決我自己 – user2412197

+0

請調查您的PostData並在調試後分享確切的錯誤。 –

回答

0

請看着你POSTDATA。這裏舉例說明你的數據應該如何格式化。

var PostData= new 

      { 
       data = {your custom data you want to send}, 
       to = deviceId, 

       notification = new 

       { 

        body = "Got notify", 

        title = Alert, 

       }, 
       priority ="normal" 
      }; 

      var serializer = new JavaScriptSerializer(); 

      var json = serializer.Serialize(PostData); 

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

而且你可以通過調試你的代碼來分享確切的錯誤。希望以上代碼可以解決您的問題。