1

I am having trouble sending push notifications via Firebase through HTTP Request to my iOS device after the app is killed. When the app is in the foreground or active in the background everything work as expected. But if I kill the app it won't work. I am able to send notifications to my app through the Firebase console if the app is killed, so I believe something must be wrong with the code I am using.

This is my code for sending the push notification:

    private void SendPushNotification(string devicetoken, string header, string content, string pushdescription)
    {
        var textNotification = new
        {
            to = devicetoken,
            notification = new
            {
                title = header,
                text = content,
                content_available = true,
                sound = "enabled",
                priority = "high",
                id = pushdescription,
            },
            project_id = "rrp-mobile",
        };

        var senderId = "212579566459";
        var notificationJson = Newtonsoft.Json.JsonConvert.SerializeObject(textNotification);
        using (var client = new WebClient())
        {
            client.Encoding = Encoding.UTF8;
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.Headers[HttpRequestHeader.Authorization] = "key=AIfrSyAtgsWCMH4s_bOyj-Us4CrdsifHv-GqElg";
            client.Headers["Sender"] = $"id={senderId}";
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.UploadString("https://fcm.googleapis.com/fcm/send", "POST", notificationJson);
        }
    }

Am I forgetting something here? This works for sending push notifications to Android devices both in foreground, background and when app is killed, and like I said also to iOS devices in foreground and background.

The only issue is sending push notifications to iOS devices when the app has been killed. Does anyone have any idea as to how I would solve this issue?

1 Answers1

1

I just realized my mistake, and it was very simple. I am posting this here because I believe this may be an easy thing to miss.

    var textNotification = new
    {
        to = devicetoken,
        notification = new
        {
            title = header,
            text = content,
            content_available = true,
            sound = "enabled",
            **priority = "high",**
            id = pushdescription,
        },
        project_id = "rrp-mobile",
    };

You need to make sure that the priority property is defined outside the "notification" scope, like this:

    var textNotification = new
    {
        to = devicetoken,
      **priority = "high",**
        notification = new
        {
            title = header,
            text = content,
            content_available = true,
            sound = "enabled",
            id = pushdescription,
        },
        project_id = "rrp-mobile",
    };

This will make your push notifications deliver even if the app is killed.

  • 2
    This is indeed a common mistake: without sending the message as high priority it is likely (but not guaranteed) to be dropped or delivered much later. See http://stackoverflow.com/questions/37332415/cant-send-push-notifications-using-the-server-api/37550067#37550067 – Frank van Puffelen Nov 04 '16 at 13:54
  • How to execute some code after receiving the push notification. Like replying back to the server with some data after receiving the push notification on the killed ios app via FCM or APNS? – Shubham1164 Feb 11 '19 at 16:58