简体   繁体   English

使用 c# .net 发送带有图像的 GCM 推送通知(适用于 android 应用程序)

[英]sending GCM push notification with image using c# .net (for android application)

i am using this c# code to send push notification using GCM site but i also want to send image with notification title and message.我正在使用此 c# 代码使用 GCM 站点发送推送通知,但我也想发送带有通知标题和消息的图像。 please tell me how this can be achieved.请告诉我如何做到这一点。 i am trying to add "data.image" parameter to add image with notification but on getting notification image is not getting added.我正在尝试添加“data.image”参数以添加带有通知的图像,但在获取通知图像时未添加。

i have also passed "data.image" with image url that can be accessed globally but nothing is happening.我还通过了“data.image”和可以全局访问的图像 url,但什么也没有发生。

 var applicationID = "some_id";
            // SENDER_ID is nothing but your ProjectID (from API Console- google code)//                                          
            var SENDER_ID = "My-Id";
            var value = message;
            var title = titl;
            //var image = "https://dl.dropboxusercontent.com/u/887989/antshot.png";


            //var imgstat = "http://justcash.co.in/img/logo0011.png";
            WebResponse tResponse;
            WebRequest 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}", applicationID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
            string postData = "collapse_key=score_update&time_to_live=1000&delay_while_idle=1&data.title=" + title + "&data.message="
                 + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regId + "&data.image=" + "some-image-source";

            //string postData = "collapse_key=score_update&time_to_live=1000&delay_while_idle=1&data.title=" + title + "&data.message="
            //    + value + "&data.time=" + System.DateTime.Now.ToString() + "&data.image=" + image + "&registration_id=" + regId + "";


            Console.WriteLine(postData);
            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            tResponse = tRequest.GetResponse();
            dataStream = tResponse.GetResponseStream();

            tReader = new StreamReader(dataStream);

            sResponseFromServer = tReader.ReadToEnd();
            tReader.Close();
            dataStream.Close();
            tResponse.Close();

You cannot directly send an image in GCM, you need to add url of the image in data, which handled on the receiver side to show result GCM中不能直接发送图片,需要在data中添加图片的url,由接收端处理以显示结果

GCM are limited in size and works as JSON GCM 大小有限,可作为 JSON 使用

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

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