How to send notification from C# Console application

后端 未结 2 620
野性不改
野性不改 2020-12-29 16:42

I want to create console application which is used to send notifications to different mobile devices via Goolge Firebase Notifications,

I have seen the code from lin

相关标签:
2条回答
  • 2020-12-29 16:57

    Yes, Duston is right, I am using wrong value in variable 'applicationID'

    Actually I am using mobilesdk_app_id value instead of api_key current key thats why it is giving 500 error

    Now it is working fine :)

    Thanks Json, Francis Lord for your efforts :)

    0 讨论(0)
  • 2020-12-29 17:13

    Here is the code to send notification using C#, I have made it working

     WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                            tRequest.Method = "post";
                            tRequest.ContentType = "application/json";
                            var objNotification = new
                            {
                                to = notification.DeviceToken,
                                data = new
                                {
                                    title = notification.NotificationTitle,
                                    body = notification.NotificationBody
                                }
                            };
                            string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
    
                            Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
                            tRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
                            tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                            tRequest.ContentLength = byteArray.Length;
                            tRequest.ContentType = "application/json";
                            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 responseFromFirebaseServer = tReader.ReadToEnd();
    
                                            FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
                                            if (response.success == 1)
                                            {
                                                new NotificationBLL().InsertNotificationLog(dayNumber, notification, true);
                                            }
                                            else if (response.failure == 1)
                                            {
                                                new NotificationBLL().InsertNotificationLog(dayNumber, notification, false);
                                                sbLogger.AppendLine(string.Format("Error sent from FCM server, after sending request : {0} , for following device info: {1}", responseFromFirebaseServer, jsonNotificationFormat));
    
                                            }
    
                                        }
                                    }
    
                                }
                            }
    

    Here is class FCMResponse used in this code to store response sent from FMServer

     public class FCMResponse
    {
        public long multicast_id { get; set; }
        public int success { get; set; }
        public int failure { get; set; }
        public int canonical_ids { get; set; }
        public List<FCMResult> results { get; set; }
    }
      public class FCMResult
    {
        public string message_id { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题