Reception Handler

The application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method is called when the device receives a push notification.

This method can basically receive notifications in the following situations:

  • A notification arrives when the mobile app is running in the foreground. The method is called regardless of the presence of the content-available key.

  • A notification with the content-available key arrives when the mobile app is running in the background.

  • iOS 8 and iOS 9 only: The mobile app comes to the foreground because the user tapped a notification in Notification Center.

For more information about the handler's behaviors including exceptions, see Combinations of Reception Methods. Depending on the situation, the handler might be called multiple times and a user action might call the user action handler.

If the content-available key is set:

  • On iOS 8 or later, the method is called when the mobile app is in the background regardless of the setting of "Background App Refresh" in the "Settings" screen of iOS.
  • On iOS 7 or earlier, "Background App Refresh" must be on so that the method can be called.

This method is not called for local notifications but remote notifications only.

See the sample code below with the handler:

Swift 3:

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

  let message = KiiReceivedMessage(fromAPNS: userInfo)
  // Do something.

  completionHandler(.newData)
}

Objective-C:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result)) completionHandler {
  NSLog(@"Received notification: %@", userInfo);

  KiiReceivedMessage *message = [KiiReceivedMessage messageFromAPNS:userInfo];
  // Do something.

  completionHandler(UIBackgroundFetchResultNewData);
}

The APNs payload is stored in the userInfo variable as [AnyHashable: Any]. You can get the payload content by passing this variable to the KiiReceivedMessage class.

At the position of comment // Do something., reference the KiiReceivedMessage instance and perform processes based on the Kii Cloud features. For using the KiiReceivedMessage class, see Push to App Notification, Push to User Notification, and Direct Push Notification.

The completionHandler block is called at the end of the process. This block must be called within 30 seconds after the method is executed.

The completionHandler block takes one of the UIBackgroundFetchResult enumeration cases. One of the following cases is returned when a push notification is processed. iOS uses this information for handling the background processing.

Case Description
newData The new data was successfully processed. Specify this case when the arrived notification was successfully processed.
noData There was no data. Specify this case when the notification was successfully received but it did not contain data to process and the data processing was skipped.
failed Processing of the data failed.

It is not necessary to implement the application(_:didReceiveRemoteNotification:) method that is deprecated in iOS 10. The application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method in the above sample code have the same capability. If you implement both, the application(_:didReceiveRemoteNotification:) method will not be called.