MHugh
MHugh

Reputation: 465

How to respond to iOS APNS events when iOS App is in the background or when not running

I am sending notifications and data messages (non-notification) over APNS from FCM to my iOS App, and everything works fine in the foreground.

I would like to be able to trigger code when the App is in the background.

I already have "Enable Background Modes" "Remote Notifications" set in info.plist

However the following message does not trigger until I bring the App into the foreground

    message = new Message()
    {
        Apns = new ApnsConfig()
        {
                Headers = new Dictionary<string, string>()
            {
                { "content_available", "true" },
             },
        },
        Data = new Dictionary<string, string>()
        {
             {"temperature",     "warm"},
        },
        Topic = "thetopic",
     };

1) can anyone tell me why this does not run immediately. I tried adding

{ 
    "apns-priority", "10" 
},

but this makes no difference, and indeed Local and Remote Notification Programming Guide

states that

It is an error to use this priority for a push notification that contains only the content-available key.

What am I missing here?

  1. Is there any way at all in iOS that it is possible to cause an non-running App to run up in response to an APNS notification or message, without involving the user?

  2. Is it possible under iOS to have an App permanently running (i.e. autostart) so that it can always respond to a notification or message?

Upvotes: 0

Views: 496

Answers (2)

Monet_z_Polski
Monet_z_Polski

Reputation: 366

You are sending "data message" - this will not work for apps that are not running. This is very poorly documented but you have to send "notification message" to awake your app. Just add

"notification":{
       "title" : "title",
       "body" : "body"
 },

to your message to get it to work

Upvotes: 2

Arik Segal
Arik Segal

Reputation: 3031

You should consider creating a UNNotificationServiceExtension for that purpose.

See: https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension

This is in fact a mini-app that is launched each time a notification is received, in which you can put your code.

Upvotes: 1

Related Questions