繁体   English   中英

从C#服务器发送FCM数据有效载荷通知到android?

[英]Send FCM data payload notification to android from C# server?

我正在尝试使用FCM从C#服务器将通知推送到Android设备。我正在使用下面提到的代码发送通知,它工作得很好,但是我也需要发送数据有效载荷,但我不知道如何实现。

public static void SendPushNotification(String user_id,string not_title)
        {
            try
            {
                string applicationID = "AAAAjpeM.......";
                string senderId = ".......";
                string deviceId = user_id;
                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                var data = new
                {
                    to = deviceId,
                    notification = new
                    {
                        body = not_title,
                        title = "ABC",
                        sound = "Enabled"
                    }
                };

                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(data);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
                tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                tRequest.ContentLength = byteArray.Length;

                using (Stream dataStream = tRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    using (WebResponse tResponse = tRequest.GetResponse())
                    {
                        using (Stream dataStreamResponse = tResponse.GetResponseStream())
                        {
                            using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = tReader.ReadToEnd();
                                string str = sResponseFromServer;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

我已经尝试过了,但是没有用。

var data = new
                {
                    to = deviceId,
                    data = new
                    {
                        body = not_title,
                        title = "Avicenna",
                        payload="1",
                        sound = "Enabled"
                    }
                };

尝试这个:

var data = new
                {
                    to = deviceId,
                    notification = new
                    {
                        body = not_title,
                        title = "ABC",
                        sound = "Enabled"
                    },  
                    data = new
                    {                        
                        payload="1"
                    }
            };

暂无
暂无

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

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