FCM custom notification for ios

2020-07-18 05:31发布

问题:

I know this question have been ask a lot of time in android but not ios. So I already test out push notification with FCM it work fine. Mine problem is wether it's a way to create custom notification rather than letting the system to handle the notification with the notification payload?

回答1:

-Users can send data messages with FCM.What you have to send is,

Sample PostMan Json data:

{ "data": {
    "any key": "any value",
    "any key 2": "any value 2"
  },
  "to" : "token received to mobile from FCM"
}

Payload inside "data" can change according to the requirement.

Mobile end:

application(_:didReceiveRemoteNotification:) override method will get fire.So it is unto developer to extract data from the userInfo property in iOS and use it.



回答2:

For ios using c# working code:-

  public bool NotificationToiosDevice()
    {
        bool str;
        try
        {
            //Create the web request with fire base API  
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            //serverKey - Key from Firebase cloud messaging server  
            tRequest.Headers.Add(string.Format("Authorization: key={0}", "AAAALNl-P7o:APA91bE38khU73UTdIj7L6LvVS3uI6f47REmxl.................................."));
            //Sender Id - From firebase project setting  
            tRequest.Headers.Add(string.Format("Sender: id={0}", "12345"));
            tRequest.ContentType = "application/json";

            var payload = new
            {
                to = "dDDbFltwE5o:APA91bFmZZ-c1pNp................",

                priority = "high",
                content_available = true,
                notification = new
                {
                    title =  "Title Of Notification",

                },
                data = new
                {
                    body = new { Parameter1 = "FirstParameterValue", Parameter2 = "SecondParameterValue" },

                },
            };
            string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(payload);

            Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
            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())
                    {
                        if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = tReader.ReadToEnd();
                                // str = sResponseFromServer;
                            }
                    }
                }
            }
            str = true;
        }
        catch (Exception ex)
        {
            str = false;
        }
        return str;
    }