none
push notification to android from c# RRS feed

  • Question

  • The below code i found in some websites but it always return error 

    multicast_id":6719893729607364541,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

    Code is:

    WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            //serverKey - Key from Firebase cloud messaging server  
            tRequest.Headers.Add(string.Format("Authorization: key={0}", "Server key"));
            //Sender Id - From firebase project setting  
            tRequest.Headers.Add(string.Format("Sender: id={0}", "Sender Id"));
            tRequest.ContentType = "application/json";
            var payload = new
            {
                to = "//topics//news", // even "/topics/news:

                priority = "high",
                content_available = true,
                notification = new
                {
                    body = "Test",
                    title = "Test",
                    badge = 1
                },
                data = new 
                {
                    key1 = "value1",
                    key2 = "value2"
                }

            };

            string postbody = JsonConvert.SerializeObject(payload).ToString();
            Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
            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())
                    {
                        if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = tReader.ReadToEnd();
                                //result.Response = sResponseFromServer;
    this.Label.Text = sResponseFromServer;
                            }
                    }
                }
            }
    Saturday, December 28, 2019 10:47 PM

All replies

  • Hi ikrami Sami,

    Thank you for posting here.

    I have a question and want to confirm.

    There are many ways to push notifications to Android, which one are you using?

    Usually, our engineers use the following one.

    Remote Notifications with Firebase Cloud Messaging.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 30, 2019 6:34 AM
  • Did you actually register with Firebase and get your own registration token?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, December 30, 2019 9:32 PM
  • My issue is how to get Registration token, i already register in Firebase and i can send notification from Firebase
    Tuesday, December 31, 2019 9:18 AM
  • public class FCMPushNotification {  
        public FCMPushNotification() {  
            // TODO: Add constructor logic here  
        }  
        public bool Successful {  
            get;  
            set;  
        }  
        public string Response {  
            get;  
            set;  
        }  
        public Exception Error {  
            get;  
            set;  
        }  
        public FCMPushNotification SendNotification(string _title, string _message, string _topic, string deviceId) {  
            FCMPushNotification result = new FCMPushNotification();  
            try {  
                result.Successful = true;  
                result.Error = null;  
                // var value = message;  
                string serverKey = "Your server key";  
                string senderId = "your sender id";  
                var requestUri = "https://fcm.googleapis.com/fcm/send";  
                WebRequest webRequest = WebRequest.Create(requestUri);  
                webRequest.Method = "POST";  
                webRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));  
                webRequest.Headers.Add(string.Format("Sender: id={0}", senderId));  
                webRequest.ContentType = "application/json";  
                var data = new {  
                    to = deviceId, // this if you want to test for single device  
                        //                        to = "/topics/" + _topic, // this is for topic  
                        priority = "high",  
                        notification = new {  
                            title = _title,  
                                body = _message,  
                                show_in_foreground = "True",  
                                icon = "myicon"  
                        }  
                };  
                var serializer = new JavaScriptSerializer();  
                var json = serializer.Serialize(data);  
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);  
                webRequest.ContentLength = byteArray.Length;  
                using(Stream dataStream = webRequest.GetRequestStream()) {  
                    dataStream.Write(byteArray, 0, byteArray.Length);  
                    using(WebResponse webResponse = webRequest.GetResponse()) {  
                        using(Stream dataStreamResponse = webResponse.GetResponseStream()) {  
                            using(StreamReader tReader = new StreamReader(dataStreamResponse)) {  
                                String sResponseFromServer = tReader.ReadToEnd();  
                                result.Response = sResponseFromServer;  
                            }  
                        }  
                    }  
                }  
            } catch (Exception ex) {  
                result.Successful = false;  
                result.Response = null;  
                result.Error = ex;  
            }  
            return result;  
        }  
    } 


    Tuesday, December 31, 2019 9:30 AM
  • return message:

    {"message_id":5609692993187881376}

    but no notification received 

    Tuesday, December 31, 2019 10:16 PM