我正在尝试使用C#推送GCM。我成功将消息发送到手机。但只是整个字符串连同参数出现在手机上,而不是'消息'仅参数。
即:对于我的消息字符串:" gcmAction = gcmActionFeedback,title =请提供反馈,消息=您的反馈请"
在电话通知中,我能够看到" gcmAction = gcmActionFeedback,title =请提供反馈,消息=您的反馈请"在哪里我应该看到"您的反馈请"只要。
标题也没有显示出来。而当我通过http://gcm-alert.appspot.com/发送相同的GCM消息时,一切似乎都很完美。
Plz plz帮助。这是代码
public string SendNotification2(string deviceId, string message)
{
string GoogleAppID = "AIza.......xkQk";
var SENDER_ID = "6666666666";
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=" + deviceId + "";
Console.WriteLine(postData);
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();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
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=" + deviceId + "";
Console.WriteLine(postData);
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();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
答案 0 :(得分:1)
您不能将所有字段都放入一个简单的消息字符串中。如果消息是字符串,android将完全按原样显示字符串。
因此,而不是...data.message=" + value +...
将您的值分成您想要的不同字段(gcmAction,title,...),并将它们相应地添加到网址
string msg = "foo", title="bar", gcmAction="bla";
string postdata = "....&data.message="+msg+"&data.title="+title+"&data.gcmAction="+gcmAction +"&..."
我不知道所需和/或支持的字段的确切名称,您可能需要在文档中查找它们。
或者作为替代方法:您可以创建一个具有相应有效负载的JSON对象,并在正文中传输此playload,如this blog
所示