Implementing the Initialization and Reception Processes

In order to use APNs, implement the APIs of the notification framework of iOS for initializing the push notification feature and preparing the handlers in your mobile app.

The basic implementation method is based on that for iOS push notification. For more information, refer to the iOS documentation and general technical information on the Internet.

To receive push notifications, you need to implement the following process and handlers.

  • Initialization process

    Initialize APNs and install the device to Kii Cloud. If you have taken the tutorial, you have implemented the push notification feature. See the linked topics if you need to use actionable notifications and know more about device installation.

    To support different versions of iOS, two versions of sample code are provided for initializing APNs. The UNUserNotificationCenter class is used for iOS 10 or later while the UIUserNotificationSettings class is used for iOS 8 and iOS 9. Some of the handler calls explained below will change depending on which initialization API is used.

  • Reception handler

    This handler is called when the mobile app directly receives a push notification when it is running in the foreground or background.

    Prepare this handler if you want to receive silent notifications.

  • User action handler

    This handler receives notification actions. With iOS 10 or later, this handler also receives push notifications that are reacted through Notification Center in addition to notification actions.

The table below shows the APIs to implement and the supported iOS versions. This guide provides sample code for each version so that you can run your mobile app on iOS 8 or later.

Related topic API iOS 8/9 iOS 10 or later Description
Initializing the push notification Initialization API of the UNUserNotificationCenter class N/A Yes This API initializes the push notification feature for iOS 10 or later.
Initialization API of the UIUserNotificationSettings class Yes Deprecated This API initializes the push notification feature for iOS 8 and iOS 9. Implement this API to support environments before iOS 10.
Reception handler application(_:didReceiveRemoteNotification:
fetchCompletionHandler:)
Yes Yes This handler is for push notifications. You can use this method regardless of the iOS version.
application(_:didReceiveRemoteNotification:) Yes Deprecated This is a legacy reception handler. This method is unnecessary if the above method with fetchCompletionHandler: is implemented.
User action handler userNotificationCenter(_:didReceive:
withCompletionHandler:)
N/A Yes This handler is called when a push notification is received via Notification Center in a case that the UNUserNotificationCenter class is used for initialization.
application(_:handleActionWithIdentifier:
forRemoteNotification:withResponseInfo:
completionHandler:)
Yes Deprecated This handler is called when an actionable notification is received in a case that the UIUserNotificationSettings class is used for initialization. Implement this method to support environments before iOS 10.

These handlers are called by different triggers depending on the situation. Different payload content and user actions can cause the methods to be called twice and not to be called at all. Kii has tested different combinations of payload keys and user actions with real apps. For the test result, see Combinations of Reception Methods as a reference for implementation and testing.

You can implement your mobile app in an efficient way, for example, by letting the reception handler and the user action handler call a common method that contains necessary processes. Depending on the payload keys in use, you might need a mechanism to eliminate duplication in processes.