Search code examples
firebase-cloud-messagingandroid-push-notification

Firebase can not send push notification to android app


I am successfully sent push notifications to FireBase Cloud Messaging(FCM) from server in order to send them to android device. But I don't know why FCM does not send those notifications to device.

My code is as below,

Notification to FCM from server(C#)

public static void SendPushNotification()
        {
            string serverKey = "AAAA...z";

            try
            {
                var result = "-1";
                var webAddr = "https://fcm.googleapis.com/fcm/send";

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"to\": \"f..A:..p_G\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}";
                    streamWriter.Write(json);
                    streamWriter.Flush();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }    
        }

My response for the above request

{"multicast_id":5002368547300,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14200031%09c4rrr5787egg"}]}

I am assuming that once FCM receives new notifications, it will push those notifications to respective android devices.

But for me, it is not working.


Solution

  • I got a solution by referring the below link

    Firebase push notification not showing on phone despite successful response

    Once I changed the 'json' string as mentioned in above link, it is working.