Notification sent from vs c # using firebase

0

I am new to learning Firebase cluod messaging, I have an app developed in android studio configured to receive fcm notifications, everything is fine here, since the firebase console sends messages and they are received without problem. On the other hand I need that in a console or winform application of visual studio c # or vb.net also be able to send notifications via fcm, this is what I can not accomplish there are "Firebase-Cloud-Messaging-Net" libraries of the nuget package but they are not used, do not exist or do not find supporting documentation for their use. The closest code for me to find it in stack and is the following

  try
        {
            var applicationID = "AAAAjPPCpVE:APA9XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            var senderId = "605385041233";
        string deviceId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        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 = "This is the message Body",
                title = "This is the title of Message",
                icon = "myicon"
            },
            priority = "high"

        };

        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(data);
        Byte[] byteArray = Encoding.UTF8.GetBytes(json);
        tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
        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.Write(sResponseFromServer);

                        textBox1.Text=sResponseFromServer;
                    }
                }
            }
        }
    }

    catch (Exception ex)
    {
        textBox1.Text=(ex.Message);
    }


}

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    }

This returns me the error

{"multicast_id":5407675484915313566,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MissingRegistration"}]}.

and I calculate that with this code I am missing the token or some authorization.

Any example that works? or a solution for my code?

since thank you very much

    
asked by Juan Borio 23.06.2017 в 18:24
source

1 answer

0

You are calling the service correctly but the order is poorly formed, the structure should be:

{
  "to": "<id del receptor>",
  "data": {
    "message": "Hola mundo!",
   }
}

so I see these including the field to within data and that's why you do not recognize the ID you're sending.

    
answered by 27.06.2017 в 18:08