Notifications with FireBase Cloud Messaging and C sharp

0

I have a problem since yesterday and I want to know if it is really possible to implement it.

I have an application in C # desktop and another in Android. What I would like to do is, in the case something is done in the desktop application (be it registration, update or deletion), be notified by FireBase to my Android application.

What I could do in my tests is to notify my Android APP from a FireBase console event.

By the following code, with the credentials of my FireBase account I have sent a "notification", without receiving anything in my Android APP, but receiving an answer:

C # notification code:

private static string SendPushNotification()
    {
        string response;

        try
        {
            // From: https://console.firebase.google.com/project/x.y.z/settings/general/android:x.y.z

            // Projekt-ID: x.y.z
            // Web-API-Key: A...Y (39 chars)
            // App-ID: 1:...:android:...

            // From https://console.firebase.google.com/project/x.y.z/settings/cloudmessaging/android:x,y,z
            // Server-Key: AAAA0...    ...._4

            string serverKey = "AAA......5"; // Something very long
            string senderId = "18......7";
            //string deviceId = "dj9...c:APA...    .....WTw"; // Also something very long, 
                                                            // got from android
            string deviceId = "/topics/android";             
                                                            // Use this to notify all devices, 
                                                            // but App must be subscribed to 
            // topic notification
            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 = "Greetings",
                    title = "Augsburg",
                    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}", serverKey));
            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();
                            response = sResponseFromServer;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            response = ex.Message;
        }

        return response;
    }

Answer

"{\"message_id\":82xxxxxxxxxxxxxxxxx}"

"{\"multicast_id\":8580xxxxxxxxxx,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:152xxxxxxxxx%0000000000000000\"}]}"

As far as I have been able to review the documentation, there is nothing official for C #. Am I on the right track? Is it possible to do this or do I have to do something else?

Punctual, I need to notify changes in my Android APP from my C # application.

I have:  - A WinForm application made in Visual Studio C #  - An account in FireBase CLoud Messaging  - An APP made in Android Studio.

Thanks for any help.

    
asked by Kenny 20.04.2018 в 01:40
source

1 answer

0

If there is a nugget for c # that is maintained by Microsoft, it is a wrapper of the library official for both Android and iOS.

It is important that in your app you give them the permissions to receive notifications, enable the methods to manage them. In this guide They explain step by step how to configure your app to be able to receive.

It is important that if you are going to handle notifications to specific devices you must obtain the token (which is not more than a telephone ID) and link it to the user of your app in order to send the notification to that token (ie, it was user)

    
answered by 20.04.2018 в 02:01