Now we will start implementing the initialization code and the reception handler.
Registering the device to APNs
Let us add initialization code for push notification to the application:didFinishLaunchingWithOptions: method in the AppDelegate class. You need to import the UserNotifications framework to execute the APIs that you add.
See the final source code below. This sample also includes the code that we had implemented to initialize Kii Cloud.
This code asks the user for permission to enable the push notification feature and registers the device to APNs. This code declares to use the three notification styles: alerts (.alert), sounds (.sound), and icon badges (.badge). The prompt message includes these styles accordingly.
The APIs used in the initialization code are different between iOS 10 or later and iOS 9 or earlier.
iOS 10 or later
The requestAuthorization method of the UNUserNotificationCenter class is called to ask the user for permission.
The completionHandler is called after the user responds. The granted argument indicates if the user enabled the push notification feature. If the user did, the registerForRemoteNotifications method is called to register the device to APNs.
iOS 8/iOS 9
For the OSes earlier than iOS 10, execute the registerUserNotificationSettings method that displays the prompt for permission to the user and the registerForRemoteNotifications method that registers the device to APNs.
Installing the device token to Kii Cloud
Next, associate the user with the device token obtained from APNs and register the token to Kii Cloud.
After the device is successfully registered to APNs, we call the application:didRegisterForRemoteNotificationsWithDeviceToken: method with the device token from APNs as the argument. If the registration fails, we call the application:didFailToRegisterForRemoteNotificationsWithError: method.
The above process is implemented as below:
After the device is successfully registered to APNs, call the application:didRegisterForRemoteNotificationsWithDeviceToken: method. Then log in the user and install the device.
Log in to Kii Cloud with the authenticate method of KiiUser. Here we use the user that you set up when creating the iOS app. Specify the username and password of the test user in username and password. The sample code simply logs in the user with the fixed username user1 and the password 123ABC.
After the user successfully logs in, install the device token from APNs to Kii Cloud with the install method of KiiPushInstallation. The logged-in user and the device token are associated on Kii Cloud because the method is executed with the logged-in user's privilege. From now on, this device receives push notifications addressed to this user.
Note that the sample code installs the device with APNs in development mode. When you want to receive push notifications in production mode, set andDevelopmentMode to false (NO).
Installing at each startup
The device token needs to be installed each time the device starts up. If you move the code for getting the device token and installing it to Kii Cloud to other class, make sure to execute the code each time the mobile app starts.
The Local and Remote Notification Programming Guide by Apple also mentions that your app must register with APNs each time it launches. Kii Cloud might delete device tokens if it detects that the tokens are invalid in APNs. If the mobile app does not install a new device token to Kii Cloud, the device will not receive push notifications any longer.
Adding the reception handler
Next, let us implement the reception handler that will be executed when the device receives a push message. When you develop a real mobile app, you would implement some app features that use data extracted from messages, though this sample code just prints log messages.
When you implement the push notification feature in your mobile app, see Implementing the Initialization and Reception Processes for considering implementation of other handlers. The method introduced in this topic is sufficient if you just want to test the push notification feature.
This is all for the implementation. Let us move to the next step: Send Test Messages to check if the push notification really works.