iOS push notifications and AppDelegate methods behaviour
Asked Answered
M

2

11

I did some research on lots of stackoverflow issues and websites in trying to figure out how do the iOS push notifications influence AppDelegate lifecycle methods and when is which method (not) getting triggered. Main focus of the research was on "standard" iOS push notifications (with alert field) and silent ones (with just content-available set to 1) and on AppDelegate's application:didReceiveRemoteNotification and application:didFinishLaunchingWithOptions methods.

I don't want to ask lots of questions for different scenarios, but would rather try to write down the statements about different test cases I tried and ask you after that:

Is there any statement that is wrong and if yes, which one and why?


Scenario 1: App has been used and put to background by tapping the home button.

  1. If standard push notification is sent, in the moment of push notification arrival, none of the methods gets triggered, app remains inactive in background. Once push notification has been tapped and app got opened because of it, application:didReceiveRemoteNotification method got called and application:didFinishLaunchingWithOptions doesn't get called. I tested this scenario right after putting the app to background and also after an app being in background for more than an hour - same behaviour. I guess that if for some reason iOS decided to kill my app while being in background, this test case becomes like Scenario 2, statement 1 from below, right?

  2. If silent push notification is sent, in the moment of the push notification arrival, application:didReceiveRemoteNotification method got called and application:didFinishLaunchingWithOptions doesn't get called.


Scenario 2: App has been used and killed by swiping it out of the list of running apps.

  1. If standard push notification is sent, in the moment of push notification arrival, none of the methods get triggered, app remains killed. Once push notification has been tapped and app got opened because of it, application:didReceiveRemoteNotification method got called and application:didFinishLaunchingWithOptions doesn't get called.

  2. If silent push notification is sent, none of the methods gets triggered since silent push notifications fail to be sent to the app that got killed. After opening an app after notification is sent, application:didFinishLaunchingWithOptions gets called as part of the normal flow and without any push notification information. application:didReceiveRemoteNotification doesn't get called.


If you can maybe think of some other real life scenarios that I maybe forgot to mention, I would be really grateful to find out about them and what happens in those cases.

Cheers


Update #1

Thanks to Sandeep Bhandari for the update and additional scenarios. I forgot to mention in my original question that I was exploring scenarios in which application is arriving to the app that is currently not in the foreground for whatever reason.

Adding Sandeep's scenarios to the list:


Scenario 3: App is being used and push notification arrives.

  1. If standard push notification is sent application:didReceiveRemoteNotification method will get called. application:didFinishLaunchingWithOptions will not get called.

  2. If silent push notification is sent application:didReceiveRemoteNotification method will get called. application:didFinishLaunchingWithOptions will not get called.


Scenario 4: App being alive in background.

  1. If standard push notification is sent application:didReceiveRemoteNotification method will get called. application:didFinishLaunchingWithOptions will not get called.

  2. If silent push notification is sent application:didReceiveRemoteNotification method will get called. application:didFinishLaunchingWithOptions will not get called.


Messroom answered 24/2, 2017 at 15:49 Comment(1)
I would like to make a minor comment... you say "killed by swiping it out of the list of running apps", this is not quite accurate. The list does not show only running apps. It's a recently-used list. Apps can still be in this list even if they have been terminated.Senega
M
14

From an experience and digging alot on the iOS push notification. App being in foreground or alive in background. both situations triggers same delegate methods. only didReceiveRemoteNotification.

The silent push notification have a different handler: (content-available 1 means silent notification)

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    }

When app is dead. didReceiveRemoteNotification never called for regular push notification. It must be handled in didFinishLaunchingWithOptions as following:

// handle notification when app is closed.
let notification = launchOptions?[.remoteNotification]
if notification != nil {
    self.application(application, didReceiveRemoteNotification: notification as! [AnyHashable : Any])
}

Additional information:

To test receiving push notification when app is killed. remove from the list that appears when double tapping home button?

The proper way to see the logging and do debugging is by editting the run scheme and selecting Wait for executable to be launched:

enter image description here

Run the app from the xcode. Then send push notification from server and then tap the notification from notification center.

Moussaka answered 24/2, 2017 at 16:36 Comment(4)
Thanks for the reply! Sending push notification when app is killed, I tested this in two different scenarios. 1) I have tested it with app in production which is supposed to trigger backend server in different ways if some of these methods is triggered and simply killing the app and sending push notifications to my device with app in production. 2) I have tested debug version of the app in exact way like you described in this screenshot: I have app already installed, kill it with swipe and then I run from Xcode with Wait for executable to be launched and send notification while waiting.Messroom
Mr. @hasan83 but isn't working for me. I mean, if I tap the app icon, the launchOptions?[.remoteNotification] won't work, because launchOptions will be nil. Your answer works only when the app is opened by tapping the notification on notification center. Is there any way to handle the scenario when the app is terminated, I received a notification and tap the app icon instead tapping the notification?Jellied
so are you saying that when the notification recieve you open the app yourslef by tapping the app icon?Moussaka
@Jellied this is not how iOS behaves. opening the app manually will not allow u to access the notification. it's impossible to access it. The app never knew about it. the only way is through an api you call to check if there is any. but that not a UX that is normally applied!Moussaka
F
1

I don't see any issue in the statement you made there, but I believe you missed to iterate over two more scenarios that I can think of.

  1. App is in foreground and receives the push notification : didReceiveRemoteNotification gets called as soon as APNS gets delievered to iOS and you can handle it by checking application state in didRecieveRemoteNotification Method.

  2. App being alive in background : I believe you are aware of background modes of iOS. If app is making use of expiration handler, app will be alive even if you put it to background by tapping on home button. Duration the app lives in background depends on various factors (some tutorials say app remains alive for 3 mins which I can't guarantee) Even in this case didReceiveRemoteNotification gets called as soon as APNS gets delievered to iOS. Only this time app wont be in foreground but yet its alive!!!

Fourierism answered 24/2, 2017 at 16:2 Comment(1)
Ah, yeah. Sorry, I actually forgot to mention that my focus was on notifications being delivered while app is not really in the foreground. Will update that on my question. And thank you for your second scenario, I forgot about it. I will update my question with it as well to have better overview which scenarios did we already elaborate. Thank you so much for your answer!Messroom

© 2022 - 2024 — McMap. All rights reserved.