GCM使用JSON发送到多个RegistrationIDs:没有通知到达 [英] GCM Using JSON to send to multiple RegistrationIDs : No notification arrives
问题描述
我使用的是谷歌Android云信息将通知发送到设备,用C#.NET。我得到的API密钥,RegistrationID和senderID(那么首先方法没有工作没有什么错GCM的配置)。
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).
下面就是我想要将消息发送到仅有1 registrationID第一种方法(检查POSTDATA,我没有使用JSON):
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() + "®istration_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.
下面是我的第二个方法,在这里我使用JSON格式发送到多个ID:
(注意该过程不采取registrationID作为一个参数,因为我把它添加到我的code申报名单)
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);
}
的方法没有给予任何错误或任何东西,但通知不在设备到达。 (我console.writelined POSTDATA和它的 http://developer.android.com /google/gcm/http.html
所以,我真的不知道该怎么解决,这将是非常容易在同一时间向用户发送通知不仅仅是发送1到每个用户每X秒。
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.
感谢。
推荐答案
修改
Request.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
到
Request.ContentType = "application/json";
请问回答这个问题。
Does answer the question.
他提供,他已经明确地暗示恰恰相反JSON数据。
He is providing JSON data where he has explicitly implied quite the opposite.
HTTP头必须包含以下标题:
The HTTP header must contain the following headers:
的Content-Type:对JSON应用程序/ JSON的;
应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8的纯文本
Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.
请参阅: https://developer.android.com/google/gcm/http。 HTML
这篇关于GCM使用JSON发送到多个RegistrationIDs:没有通知到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!