【发布时间】:2015-01-30 04:16:10
【问题描述】:
我使用 Asp.net MVC 4 Web API 作为第三方服务器来使用 GCM 为 Android 设备推送通知。它工作正常,正在生成通知,但消息为空白。我花了一整天的时间,但找不到任何解决方案,请帮助我
发送推送通知的Web API函数如下:
public Notification PushToAndroidDevice(string registrationid,string message)
{
Notification notification = new Notification();
try
{
var applicationID = "MY_APPLICATION_ID";
var SENDER_ID = "MY_SENDER_ID";
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/x-www-form-urlencoded";
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
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=" + message + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + registrationid + "";
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();
notification.Message = sResponseFromServer;
tReader.Close();
dataStream.Close();
tResponse.Close();
notification.Status = true;
}
catch (Exception ex)
{
notification.Status = false;
notification.Message = "ERROR DESCRIPTION : " + ex.Message;
}
return notification;
}
Notification 是一个有两个属性 Status bool 和 Message string 的类
public class Notification
{
public bool Status { get; set; }
public string Message { get; set; }
}
通过此代码,我可以在 android 手机上发送通知消息,但通知为空白,请帮助我......
【问题讨论】:
-
知道了......我的android代码有问题......
-
你能帮帮我吗,我有类似的功能要在我的 android 应用程序中实现。如果您还可以嵌入您的 android 代码,那将会很有帮助。
-
通过 GCM 使用 AndroidHive 推送通知 androidhive.info/2012/10/…
标签: asp.net-mvc-4 asp.net-web-api google-cloud-messaging