0

i am trying to create a messaging app using xamarin.forms. i created a send button and added the following code:

 private async void send_Clicked(object sender, EventArgs e)
{

await Task.Run(() => SendNotification(token, "title", message.Text));
}

and the sendnotification is as follows:

 public string SendNotification(string DeviceToken, string title, string msg)
        {
            var result = "-1";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
            httpWebRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
            httpWebRequest.Method = "POST";

            var payload = new
            {
                //to= "/topics/klm",
                to = DeviceToken,
                //to = "egjLx6VdFS0:APA91bGQMSSRq_wCzywNC01zJi4FBtHXrXuL-p4vlkl3a3esdH8lxo7mQZUBlrTi7h-6JXx0GrJbwc9Vx6M5Q4OV_3CArcdlP0XMBybervQvfraWvqCgaa75gu9SVzjY4V_qd36JGg4A",
                priority = "high",
                content_available = true,

               
               
                data = new
                {
                    text = msg,
                },
            };
            var serializer = new JsonSerializer();
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = JsonConvert.SerializeObject(payload);
                streamWriter.Write(json);
                streamWriter.Flush();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
            return result;
        }

this is private string webAddr = "https://fcm.googleapis.com/fcm/send";

when i click send the first time, i receive the message perfectly, when i hit send the second time, the app freezes then crashes and asks me if i want to close the app or wait. why is this happening? thanks in advance

rana hd
  • 355
  • 3
  • 18
  • 1) Probably some exception: Wrap code in try-catch (Exception ex). Breakpoint in `catch`. What is string value of `ex`? 2) See [webrequest doc](https://learn.microsoft.com/en-us/dotnet/api/system.net.webrequest.create?view=net-7.0): "WebRequest ... are obsolete. Use HttpClient instead." – ToolmakerSteve Feb 17 '23 at 20:29
  • *firebaser here* (not an answer) Calls to the FCM REST API require that you specify the FCM *server** key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. By including this key in your app itself, a malicious user can find it and you're putting your users at risk. See https://stackoverflow.com/a/37993724 for a better solution. – Frank van Puffelen Feb 17 '23 at 23:29
  • @FrankvanPuffelen thanks for your answer, yes that's right i know that this is just a trial to get the hold of it, when i get it to work correctly i'll start working on the security part – rana hd Feb 18 '23 at 06:37

0 Answers0