Enabling the Kii Push Notification Feature

To leverage the push notifications, you'll need to configure your application by following the procedures covered in this topic. To use the push notification feature, you also need the Kii Cloud SDK. If you have not installed the SDK yet, read the section Adding the Kii Cloud SDK and install the SDK first.

Kii Cloud's push notifications rely on the Firebase Cloud Messaging (FCM) and Apple Push Notification service (APNs) services. To leverage the push notification on Kii Cloud, you need to make the FCM/APNs related configuration along with Kii Cloud setting.

You can receive push notifications on Android and iOS. For iOS, you need to have real devices to receive push messages.

In this topic, we will first show the project setting required for both Android and iOS. Then we will show the Android and iOS specific setting.

Configuring your project

We first need to integrate Kii Cloud push feature into the Unity project. The procedure is the same for both Android and iOS.

  1. Create an empty game object and bind Assets/Kii/Scripts/KiiPushPlugin.cs to it with the following steps:

    First, Create an empty game object.

    Then, drag & drop KiiPushPlugin.cs onto this empty game object.

    Finally, change the name of the game object to KiiPushPlugin.

    You can drag & drop the game object to change its hierarchy, but make sure to leave the KiiPushPlugin at the root level. If you move it to the lower hierarchy, the initialized information could be lost when the scene changes.

Configuring for Android

If you are delivering your application for Android, configure your app for FCM.

This topic only outlines the configuration steps. Click the links below to see the detailed instruction of these steps. When you are done with these steps, come back to this topic and proceed.

To leverage the FCM-based push notification, you need to register your project on the Firebase console, get the server key, and register this key as the GCM key on the Kii Cloud developer portal.

  1. Create a Firebase Project

    Create a project for your application and get the server key for the push notification on the Firebase console.

  2. Configure Kii Cloud

    Register the server key on the Kii Cloud developer portal.

  3. Set the SenderID in the property of the game object on which you've bound the KiiPushPlugin.cs

    For the SenderID, set the sender ID shown in this screen on the Firebase console.

  4. Edit the Manifest file.

    Edit the file Assets/Plugins/Android/AndroidManifest.xml for Unity by replacing four __com.example.your.application.package.name__ shown below with your application package name. The package name should be the Android's "Bundle Identifier" you've specified in the "Player Settings".

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="__com.example.your.application.package.name__"
    
            (...snip...)
    
        <uses-permission android:name="__com.example.your.application.package.name__.permission.C2D_MESSAGE" />
        <permission android:name="__com.example.your.application.package.name__.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    
            (...snip...)
    
            <receiver
                android:name="com.kii.cloud.unity.GCMBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <category android:name="__com.example.your.application.package.name__" />
                </intent-filter>
            </receiver>
    
            (...snip...)
    
    </manifest>
    

    Make sure not to delete <action android:name="PACKAGE_NAME.MESSAGE" /> in the AndroidManifest.xml.

Configuring for iOS

Follow the steps below to implement the push notification feature in your Unity app.

Kii Cloud leverages the Apple Push Notification service (APNs) in its push notification feature. To use the APNs-based push notification, you need to make some APNs-related configuration and prepare your build environment besides configuring Kii Cloud.

See Overview of Push Technologies for Unity for the technical overview of APNs.

Follow the steps below for configuration.

This topic only outlines the configuration steps. Click the links below to see the detailed instruction of these steps. When you are done with these steps, come back to this topic and proceed.

  1. Publish and Build Your App in Xcode

    Before configuring APNs, save the settings of the Xcode project to the Apple server.

    To do so, publish your Unity app according to the instruction in the link destination, build your mobile app in Xcode, and run the mobile app on a real device. Information about the development certificate and such will be saved to the Apple server.

  2. Create a Certificate

    Get an SSL certificate from the Apple Developer website. This certificate is necessary to send push notifications to the mobile app.

  3. Upload the Certificate to Kii Cloud

    Pack the SSL certificate and password as a P12 file and upload it to the Kii Cloud developer portal.

  4. Customize the Push Notification Feature

    When you want to customize the types of push messages that your mobile app allows, edit the parameters of the registerForRemoteNotificationTypes: method in the Assets/Plugin/iOS/UIApplication+KiiCloud.m file.

    void registerForRemoteNotifications() {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                                           UIRemoteNotificationTypeSound |
                                                                           UIRemoteNotificationTypeAlert)];
    }
    

    For more details on the parameters, check the documentation by Apple.

    When you are using APNs, the registration is done automatically by the plugin. Note that the plugin is overwriting a portion of UIApplicationDelegate implementation in Assets/Plugins/iOS/UIApplication+KiiCloud.m. If you are using other Unity plugins that touch the following delegates, you might experience some conflicts.

    - application:didFinishLaunchingWithOptions:
    - application:didRegisterForRemoteNotificationsWithDeviceToken:
    - application:didFailToRegisterForRemoteNotificationsWithError:
    - application:didReceiveRemoteNotification:
    - application:didReceiveRemoteNotification:fetchCompletionHandler:
    

    This is all for the configuration. When you are to build the iOS executable, however, you need to perform the following Xcode set up.

  5. Configure the Project in Xcode

    Enable push notification in the published Xcode project.

    Once you make the Xcode configuration, the setting will be preserved. In rare cases, however, the setting disappears. If you get into trouble or when you are going to build an official release, double-check the Xcode setting.

Adding the initialization code

When the configuration is done, we will start implementing the application by adding the following initialization code. This applies to both Android and iOS environments.

Installing a device

Install a device in Kii Cloud by sending the device token issued by the FCM or APNs server to Kii Cloud. Once the device is installed, it is ready to receive push notifications.

For more information about device installation, see Installing a device.

To install a device, use the PushInstallation as follows. This code works for both Android and iOS:

#if UNITY_IPHONE
bool development = false;      // Choose the APNs production server for iOS.
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
#elif UNITY_ANDROID
bool development = false;      // Choose the FCM production server for Android.
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
#endif

this.kiiPushPlugin = GameObject.Find("[GAME_OBJECT_NAME]").GetComponent<KiiPushPlugin>();

// Register the device to FCM or APNs.
this.kiiPushPlugin.RegisterPush((string pushToken, Exception e0) => {
  // Authenticate the user.
  KiiUser.LogIn(USER_NAME, PASSWORD, (KiiUser user, Exception e1) => {
    // Register the device token for the logged-in user to Kii Cloud.
    KiiUser.PushInstallation(development).Install(pushToken, deviceType, (Exception e2) => {
    });
  });
});

Specify KiiPushInstallation.DeviceType.ANDROID when sending the device token for FCM and KiiPushInstallation.DeviceType.IOS when sending the device token for APNs. Also specify your environment (i.e. development or production) with the development.

The device token of FCM and APNs will be notified to the callback function by executing the RegisterPush method. Execute the PushInstallation method with them to install the device. The device installation will update the user who is bound to the device token to the current user. If the device token was bound to another user, the binding is canceled by the installation; push notifications sent to the old user will no longer be delivered to the device.

Uninstalling a device

You can uninstall a device if you want to disable the device from receiving push notifications. Once the device is uninstalled, it will no longer receive any push notifications.

#if UNITY_IPHONE
bool development = false;      // Choose the APNs production server for iOS.
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
#elif UNITY_ANDROID
bool development = false;      // Choose the FCM production server for Android.
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
#endif

// Unregister the device token for the logged-in user from Kii Cloud.
KiiUser.PushInstallation(development).Uninstall("[PUSH_TOKEN]", deviceType, (Exception e0) => {
  this.kiiPushPlugin = GameObject.Find("[GAME_OBJECT_NAME]").GetComponent<KiiPushPlugin>();
  // Unregister the device from FCM or APNs.
  this.kiiPushPlugin.UnregisterPush((Exception e1) => {
  });
});

Specify KiiPushInstallation.DeviceType.ANDROID when you are uninstalling the device token for FCM and KiiPushInstallation.DeviceType.IOS when you are uninstalling the device token for APNs. Also specify your environment (i.e. development or production) with the development.

You can also unregister FCM/APNs at the same time. To do this, execute the KiiPushPlugin's UnregisterPush method.


To continue with the push implementation, see our Managing Push Notifications section.