Create a new theme for Firebase notifications from aspx.net

2

I'm doing an app for Android and I want to be able to send notifications from an asp.net server.

Since the Google Firebase console works correctly, you can send a notification to all devices that use the application.

As I have read it is not possible to send this type of notification from outside the console, so it has occurred to me that all the devices subscribe to a topic, and send the notification to the topic.

The problem is that I want to create the theme from the server, and I have no idea. I've tried this:

    private void crearTopic()
        {
        try
            {
            var applicationID = "AAAARq_rfdY:APA92bH4GeOlB2p............";
            string deviceId = "c9yessCNTc:APNN91bE................";//Mi movil
            string url = @"https://iid.googleapis.com/iid/v1/"+ deviceId+"/rel/topics/minuevotema";
            WebRequest tRequest = WebRequest.Create("url");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));

            using (Stream dataStream = tRequest.GetRequestStream())
                {
                using (WebResponse tResponse = tRequest.GetResponse())
                    {
                    using (Stream dataStreamResponse = tResponse.GetResponseStream())
                        {
                        using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                            String sResponseFromServer = tReader.ReadToEnd();
                            //Response.Write(sResponseFromServer);
                            string respuesta = sResponseFromServer;
                            }
                        }
                    }
                }
            }

        catch (Exception ex)
            {
            string cad = ex.Message;
            }
        }

But an exception jumps, I suppose it's because of the ":" of the device ID.

Can someone help me out? or tell me some example page?

Thank you very much.

    
asked by Juanjo 20.10.2017 в 16:16
source

1 answer

0

I built my mobile app using Firebase with Xamarin Forms I attached the code that I used in my web app to send notifications in

 public void SendPushNotification(string deviceId)
    {

        try
        {

            string Server_Key = "AAAApYKXRw8:APA91bGm4FvAZTnULA1pM-qj9twpE_xutZDlLXjyiLpdpP1seNsHa0nGwvFclKsx75Kney-3WkBbCAVHeqXFbYr3QCeXWAIjSSgho2rQOd40H-Z2MjDpb5VxgKgswRKQt";

            string senderId = "71086XX047";

            // string deviceId = "cVfegfXs1K8:APA91bHUHAxQQ1j9geVrcL65j2lI6xaHS9Fm64561s-DOCTHiX0EDZhuGkdCcLJ5rji2XLshtL9ldtJ02hnjzw134l7BCRizZj22kjBu6ysaptQ596l";

            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 = "Hola",
                    title = "Notificacion1",
                    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}", Server_Key));
            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;
        }
    }

Note: deviceId is the token that gave me firebase in the mobile app.

I hope you serve, greetings!

    
answered by 23.10.2017 в 18:34