【问题标题】:Blank Message in Push Notification to Android Device Using GCM and Asp.net MVC Web API使用 GCM 和 Asp.net MVC Web API 向 Android 设备推送通知中的空白消息
【发布时间】: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() + "&registration_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


【解决方案1】:

您应该检查从 GCM 服务器接收消息的 MSGReceiver 和 MSGSerice(或相应的)类。 您检查打印并查看您是否收到空值。可能是那里使用的变量名称与您从 MVC 发送的变量名称不同。这是一个非常简短的答案。如果您还有其他问题或难以理解,请提供相关代码和文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2017-01-08
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多