0

We have an iOS App to which we send notifications via FCM.

What we would like to be able to do ideally is cause the App to run some code directly from a notification.

This seems doable if we send a data type notification and the App is in foreground.

However - how can we do this if a) the App is in background b) the App has been terminated. Is there some way to automatically run the App up, or to make the App run itself back up if it is terminated.

Thanks

MHugh
  • 455
  • 1
  • 7
  • 20

1 Answers1

1

App is in foreground

Implement didReceiveRemoteNotification delegate method in AppDelegate to handle the notification and run the code you want when app is in foreground.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    //write your code here when app is in foreground
}

App is in background

When the app is in background and user receives push notification, didReceiveRemoteNotification will get called if user taps on the notification but if the user taps on App icon then this method will not get called, In this case you have to communicate with your server or you can check the badge number with the previous value(you can store it in NSUserDefaults) and run the code you want.

App has been terminated

When the app has been terminated didReceiveRemoteNotification method will not get called, But you can check launchOptions to know weather the application has been launched from notification or not in didFinishWithLaunchOptions method in appdelegate and do your task accordingly.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
     // Do your task here
    }

}
Prakash Shaiva
  • 1,047
  • 8
  • 12
  • Thanks for those clear answers Prakash. What I would like to explore is whether I can execute code when in background mode - without having to wait for the user to interact with a notification. In my question I mentioned using "data" messages (rather than Notifications) so these would not involve the user anyway in terms of raising a notification. For example, would it be possible to handle an APNS message in code which uses one of the Background Modes in info.plist, and if so, which one would be the best fit to respond to an FCM/APNS message ? – MHugh Nov 08 '19 at 14:24
  • As long as "Enable Background Modes" for "Remote Notifications" is selected in the info.plist, non-notification (i.e. data only) FCM/APNS events *do* indeed fire the didReceiveRemoteNotification event. – MHugh Nov 12 '19 at 16:33
  • So the only way I can get code to execute in the background is if I send a silent notification. However, silent notifications are are lower priority that alert notifications, and anyway, according to the apple documentation, not guaranteed to be delivered at all necessarily. So the bottom line question here is how can I execute code in background following the sending of an alert notification? Is this even possible? – MHugh Nov 22 '19 at 17:57