0

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.

Thamarai T
  • 252
  • 1
  • 6
  • 19
  • I don't know there is anything must be done on mobile app side to receive notifications. – Thamarai T Jun 01 '18 at 07:48
  • It looks like you are s nding a data message to the device. Data messages don't automatically show notifications. You have to implement the FirebaseMessagingService and handle data messages in onMessageReceived callback – Arthur Thompson Jun 01 '18 at 20:14
  • Yes. I got it for notification messages and it is working. But I don't know how to process data messages. Can u send some sample code? – Thamarai T Jun 02 '18 at 07:16
  • See the FCM sample app that demonstrates this: https://github.com/firebase/quickstart-android/tree/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm – Arthur Thompson Jun 03 '18 at 23:32
  • There is no getData() method available. Meanwhile I got a solution from https://forums.xamarin.com/discussion/62903/on-notification-click-app-doesnt-show. – Thamarai T Jun 04 '18 at 10:01

1 Answers1

0

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.

Thamarai T
  • 252
  • 1
  • 6
  • 19
  • It may be helpful to know about the syntax from https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support – Thamarai T Jun 01 '18 at 10:23