GCM Using JSON to send to multiple RegistrationIDs : No notification arrives

╄→尐↘猪︶ㄣ 提交于 2019-12-13 05:34:36

问题


I'm using Android Google Cloud Messaging to send notifications to a device, with c#.net. I got the API Key, RegistrationID and senderID ( Well first method worked no there's nothing wrong with the configuration of GCM).

Here's the first method where I want to send the message to just 1 registrationID(check postData, i'm not using 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);
        }

So when calling it, the device successfully receives the message.

Here's my second method, where I use Json format to send to multiple IDs: (Note that the procedure doesn't take registrationID as a parameter because I added it to the list i declared in the code)

    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);
        }

The Method doesn't give any errors or anything but the notification doesn't arrive in the device. (I console.writelined postData and it's the same format of http://developer.android.com/google/gcm/http.html So I don't really know what to fix, it would be really easier to send notifications to users at the same time not just send 1 to each User every X seconds.

Thanks.


回答1:


Changing

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

to

Request.ContentType = "application/json";

Does answer the question.

He is providing JSON data where he has explicitly implied quite the opposite.

The HTTP header must contain the following headers:

Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.

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




回答2:


Use

Request.ContentType = "application/json";



回答3:


Replace this line:

Request.ContentType = "application/json";


来源:https://stackoverflow.com/questions/18508674/gcm-using-json-to-send-to-multiple-registrationids-no-notification-arrives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!