Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GCM使用JSON发送到多个注册ID:未收到通知_C#_Android_Json_Google Cloud Messaging - Fatal编程技术网

C# GCM使用JSON发送到多个注册ID:未收到通知

C# GCM使用JSON发送到多个注册ID:未收到通知,c#,android,json,google-cloud-messaging,C#,Android,Json,Google Cloud Messaging,我使用安卓谷歌云消息发送通知到一个设备,使用c#net。我得到了API密钥、RegistrationID和senderID(第一种方法很好地工作了,不,GCM的配置没有问题) 这里是第一种方法,我只想将消息发送到1个registrationID(检查postData,我没有使用Json): public void SendPushNotification(字符串regID,字符串消息) { { 字符串GoogleAppID=“APIKey”; var SENDER_ID=“SenderID”;

我使用安卓谷歌云消息发送通知到一个设备,使用c#net。我得到了API密钥、RegistrationID和senderID(第一种方法很好地工作了,不,GCM的配置没有问题)

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

public void SendPushNotification(字符串regID,字符串消息)
{
{
字符串GoogleAppID=“APIKey”;
var SENDER_ID=“SenderID”;
var值=消息;
WebRequest-tRequest;
tRequest=WebRequest.Create(“https://android.googleapis.com/gcm/send");
tRequest.Method=“post”;
tRequest.ContentType=“application/x-www-form-urlencoded;charset=UTF-8”;
添加(string.Format(“Authorization:key={0}”,GoogleAppID));
添加(string.Format(“Sender:id={0}”,Sender_id));
string postData=“collapse\u key=score\u update&time\u to\u live=108&delay\u while\u idle=1&data.message=“+value+”&data.time=“+System.DateTime.Now.ToString()+”®istration\u id=“+regID+”;
Byte[]byteArray=Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength=byteArray.Length;
Stream dataStream=tRequest.GetRequestStream();
写入(byteArray,0,byteArray.Length);
dataStream.Close();
WebResponse-treresponse=tRequest.GetResponse();
dataStream=treresponse.GetResponseStream();
StreamReader tReader=新的StreamReader(数据流);
字符串sResponseFromServer=tReader.ReadToEnd();
HttpWebResponse httpResponse=(HttpWebResponse)t响应;
字符串statusCode=httpResponse.statusCode.ToString();
tReader.Close();
dataStream.Close();
t响应关闭();
}
抓住{
抛出新的WebFaultException(“错误”,HttpStatusCode.ServiceUnavailable);
}
因此,当调用它时,设备成功地接收到消息

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

public void SendPushNotification(字符串消息)
{
字符串stringregIds=null;
List regIDs=新列表();
//在这里,我将在方法1中使用的registrationID添加到regIDs
//然后我用
stringregIds=string.Join(“\”,\”,regIDs);
//使用下面Json格式的引号和逗号连接值(如果有多个值)
尝试
{
字符串GoogleAppID=“APIKey”;
var SENDER_ID=“SenderID”;
var值=消息;
WebRequest-tRequest;
tRequest=WebRequest.Create(“https://android.googleapis.com/gcm/send");
tRequest.Method=“post”;
tRequest.ContentType=“application/x-www-form-urlencoded;charset=UTF-8”;
添加(string.Format(“Authorization:key={0}”,GoogleAppID));
添加(string.Format(“Sender:id={0}”,Sender_id));
string postData=“{\”collapse\u key\”:\“score\u update\”,“time\u to\u live\”:108,\“delay\u while\u idle\”:true,\“data\”:{“message\”:“+”value+“\”,“time\”:“+”System.DateTime.Now.ToString()+“},\“registration\u ids\”:[\“+stringregIds+”];
Byte[]byteArray=Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength=byteArray.Length;
Stream dataStream=tRequest.GetRequestStream();
写入(byteArray,0,byteArray.Length);
dataStream.Close();
WebResponse-treresponse=tRequest.GetResponse();
dataStream=treresponse.GetResponseStream();
StreamReader tReader=新的StreamReader(数据流);
字符串sResponseFromServer=tReader.ReadToEnd();
HttpWebResponse httpResponse=(HttpWebResponse)t响应;
字符串statusCode=httpResponse.statusCode.ToString();
tReader.Close();
dataStream.Close();
t响应关闭();
}
抓住{
抛出新的WebFaultException(“错误”,HttpStatusCode.ServiceUnavailable);
}
该方法没有给出任何错误或任何东西,但是通知没有到达设备。(I console.writelined postData,它的格式与 所以我真的不知道要修复什么,同时向用户发送通知会更容易,而不仅仅是每X秒向每个用户发送1个通知

谢谢。

替换此行:

Request.ContentType = "application/json";
改变

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

他回答了这个问题

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

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

内容类型:应用程序/json代表json; application/x-www-form-urlencoded;字符集=UTF-8表示纯文本

请参阅:

使用

Request.ContentType = "application/json";

tRequest.ContentType=“application/x-www-form-urlencoded;charset=UTF-8”;
在“第二种方法”中…你正在使用JSON…再次阅读文档…哦,哇,我现在要试试。谢谢!它奏效了,我太傻了。再次感谢塞尔文这并没有提供问题的答案。若要评论或要求作者澄清,请在他们的帖子下方留下评论-你可以随时对自己的帖子发表评论,一旦你有足够的评论,你将我可以。@ScottThompson当然可以。只需要一个退出编辑。@Keyner,你能详细说明一下你的答案吗?除了直截了当(虽然准确)的答案之外,一些评论会对作者有所帮助
Request.ContentType = "application/json";