简体   繁体   English

使用C#asp.net的GCM推送通知在Android设备中将null用作通知

[英]GCM push notification with C# asp.net giving null as notification in android devices

I am creating a class in C# which has a method to send push notification in android using GCM. 我正在C#中创建一个类,该类具有使用GCM在android中发送推送通知的方法。 The method is working well and also giving response from google as success. 该方法运行良好,并且也获得了Google的成功回应。 But in the android emulator the notification is coming as null. 但是在android模拟器中,通知为null。 Here is the code I am using, 这是我正在使用的代码,

public void NotifyTest(string regId)
    {
        var applicationID = "AIza*************"; 

        var SENDER_ID = "xxxxxxxxxx"; 
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
        httpWebRequest.Headers.Add(string.Format("Sender: key={0}", SENDER_ID));
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"registration_ids\":[\""+ regId +"\"]," +
                        "\"data\": { \"score\" : \"1234\"}}";
            Console.WriteLine(json);
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Console.WriteLine(result);
            }
        }
    }

The code is working without any errors and returning the response from Google too. 该代码正常工作,没有任何错误,并且也从Google返回了响应。 Please let me know the suggestions. 请让我知道建议。

You need to replace the json string as below & modify your Android code for the get message & name. 您需要按如下所示替换json字符串,并为获取消息和名称修改您的Android代码。

string json = "{\\"registration_ids\\":[\\"" + regId + "\\"]," + "\\"data\\": { \\"message\\" : \\"1234\\", \\"name\\": \\"Arvind Sharma\\"}}"; 字符串json =“ {\\” registration_ids \\“:[\\”“ + regId +” \\“],” +“ \\” data \\“:{\\” message \\“:\\” 1234 \\“,\\”名称\\“ :\\“ Arvind Sharma \\”}}“;

In Android OnMessage(in GCMIntent service) write folowin line 在Android OnMessage(在GCMIntent服务中)中写folowin行

  String message = intent.getStringExtra("score");

Happy Coading:) 快乐的单车:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM