1

I am trying to use FCM for pushing notifications to Android device from C# server.I am using below mentioned code for sending notifications and it worked perfectly fine but I need to send data payload as well but I don't know how to implement that.

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;
            }
        }

I have tried this but it did'nt works.

var data = new
                {
                    to = deviceId,
                    data = new
                    {
                        body = not_title,
                        title = "Avicenna",
                        payload="1",
                        sound = "Enabled"
                    }
                };
1
  • Are you taking into account that pure notification messages and data payload messages are handled differently when received by the Android device. Also be aware that Android and iOS handle the push notification differently. Please check the FCM documentation. Commented Dec 10, 2017 at 17:57

1 Answer 1

4

try this:

var data = new
                {
                    to = deviceId,
                    notification = new
                    {
                        body = not_title,
                        title = "ABC",
                        sound = "Enabled"
                    },  
                    data = new
                    {                        
                        payload="1"
                    }
            };
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.