0

我正在使用 Android Google Cloud Messaging 通过 c#.net 向设备发送通知。我得到了 API 密钥、RegistrationID 和 senderID(第一种方法行得通,GCM 的配置没有问题)。

这是我只想将消息发送到 1 个注册 ID 的第一种方法(检查 postData,我没有使用 Json):

    public void SendPushNotification(string regID, string message)
 {


     {
         string GoogleAppID = "APIKey";
         var SENDER_ID = "SenderID";
         var value = message;
         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}", GoogleAppID));

         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=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regID + "";
         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();

         HttpWebResponse httpResponse = (HttpWebResponse)tResponse;
         string statusCode = httpResponse.StatusCode.ToString();

         tReader.Close();
         dataStream.Close();
         tResponse.Close();
     }
        catch {
            throw new WebFaultException<string>("Error", HttpStatusCode.ServiceUnavailable);
        }

所以在调用它的时候,设备成功接收到了消息。

这是我的第二种方法,我使用Json格式发送到多个ID:(请注意,该过程不将registrationID作为参数,因为我将其添加到我在代码中声明的列表中)

    public void SendPushNotification(string message)
 {

     string stringregIds = null;
     List<string> regIDs = new List<string>();
     //Here I add the registrationID that I used in Method #1 to regIDs
     //Then I use 
     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 = "APIKey";
         var SENDER_ID = "SenderID";
         var value = message;
         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}", 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();

         HttpWebResponse httpResponse = (HttpWebResponse)tResponse;
         string statusCode = httpResponse.StatusCode.ToString();

         tReader.Close();
         dataStream.Close();
         tResponse.Close();
     }
        catch {
            throw new WebFaultException<string>("Error", HttpStatusCode.ServiceUnavailable);
        }

该方法没有给出任何错误或任何东西,但通知没有到达设备。(我 console.writelined postData 和http://developer.android.com/google/gcm/http.html格式相同 所以我真的不知道要修复什么,向用户发送通知会更容易同时不只是每 X 秒向每个用户发送 1 个。

谢谢。

4

3 回答 3

2

改变

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

Request.ContentType = "application/json";

回答问题。

他正在提供 JSON 数据,而他明确暗示的恰恰相反。

HTTP 标头必须包含以下标头:

Content-Type:JSON 的 application/json;application/x-www-form-urlencoded;charset=UTF-8 用于纯文本。

见:https ://developer.android.com/google/gcm/http.html

于 2014-11-24T19:07:53.123 回答
1

利用

Request.ContentType = "application/json";
于 2015-12-21T13:33:54.253 回答
-5

替换这一行:

Request.ContentType = "application/json";
于 2014-06-20T15:17:56.260 回答