iOS push notifications and AppDelegate methods behaviour

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.


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!!!