Push Notification Service

Update time: 2021/09/25 14:35:29

CommsEase Unity SDK 1.5.0 introduces HUAWEI and MIUI push. When users stop the progress or CommsEase server cannot be connected to client, users are notified that a message is received by system push tip service of mobile phone manufacturer. Compared with other push services (Getui, Aurora), os-level push services (Xiaomi, Huawei, etc.) are better in maintaining stable os-level connection on the corresponding vender's system, so that push messages can be received at any time. Unity SDK only provides a channel for receiving message pushes, processing of push notifications, such as in-app jump, needs to be implemented by the app itself according to the actual situation.

Vendor push Condition description Push SDK version CommsEase SDK versions
Xiaomi Supports all MIUI versions 3.6.0 1.5.0
Huawei Requires EMUI4.1 and above, Huawei Mobile Service version 2.5.2 and above 2.5.2.300 1.5.0

Based on the principle of on-demand use, CommsEase SDK does not include third-party push SDKs. Developers can import such push SDKs according to their own needs. The steps to integrate an OS-level push can be briefly summarized as follows:

  • Go to the push platform, register an account with authentication, create an application and get information such as AppID, AppKey, AppSecret
  • Import the push SDK into the application
  • Configure AndroidManifest.xml
  • Configure push certificate on CommsEase background
  • Get push token
  • After successful login, callNIM.User.UserAPI.UpdatePushTokento submit the push certificate and push token to CommsEase server

After completing the above process, install the compiled Apk to the mobile phone that supports the os-level push. After successfully logging in, terminate CommsEase process, use other accounts to send messages to the phone and check the notification bar for push messages.

The configuration for each push platform is described in detail below.

MIUI Push Notification Service

Steps to integrate Xiaomi push:

Prerequisites

  • Go to official website of Xiaomi push, finish account registration and authentication, create an application and obtain AppID, AppKey, and AppSecret.

  • Download Xiaomi push SDK and import the jar package into the project. (CommsEase IM SDK does not contain Xiaomi Push SDK, the developer needs to download it by themselves and import it into the project)

  • Go to CommsEase Admin Center and create a Xiaomi push certificate. Select the application requiring access to Xiaomi push and click Certificate Management:

Xiaomi push certificate management

Select "add certificate" in "Android push certificate", fill in the corresponding information in the pop-up box as shown in the figure below, where the application package name should be the package name of the developer's Android application, and the AppSecret should be the one obtained in the first step of creating application on the Xiaomi official website.

Add Xiaomi push certificate

Integration Procedure

Quick access only takes 2 steps. For details, please refer to Xiaomi Push Android SDK User Guide.

1. Configuration of AndroidManifest.xml

First of all, in two places in permission configuration, you need to change the original text to your APP's package name, and the items that have been configured do not need to be added.

xml<!--Configure permissions; entries configured are not required to be added -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.VIBRATE"/>

<!--Modify the package name of developer App in the following two {Your package name} -->
<permission android:name="{Your Package name}.permission.MIPUSH_RECEIVE"
            android:protectionLevel="signature" />
<uses-permission android:name="{Your package name}.permission.MIPUSH_RECEIVE" />

The following configurations are directly copied to AndroidManifest.xml, without any change.

xml<!--Configured service and receiver-->
<service
    android:name="com.xiaomi.push.service.XMPushService"
    android:enabled="true"
    android:process=":mixpush"/>
<service
    android:name="com.xiaomi.push.service.XMJobService"
    android:enabled="true"
    android:exported="false"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:process=":mixpush" />
    <!--Note: the service must be added following the version 3.0.1 (inclusive)-->
<service
    android:enabled="true"
    android:exported="true"
    android:name="com.xiaomi.mipush.sdk.PushMessageHandler" />

<service android:enabled="true"
    android:name="com.xiaomi.mipush.sdk.MessageHandleService" />
<!--Note: the service must be added following the version 2.2.5 (inclusive)-->
<receiver
    android:exported="true"
    android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
<receiver
    android:exported="false"
    android:process=":mixpush"
    android:name="com.xiaomi.push.service.receivers.PingReceiver" >
    <intent-filter>
        <action android:name="com.xiaomi.push.PING_TIMER" />
    </intent-filter>
</receiver>
<receiver
    android:name="com.netease.nimlib.mixpush.mi.MiPushReceiver"
    android:exported="true">
    <intent-filter android:priority="0x7fffffff">
        <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE"/>
        <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED"/>
        <action android:name="com.xiaomi.mipush.ERROR"/>
    </intent-filter>
</receiver>

2. Upload token and certificate

Unity Demo provides an example of getting token of MIUI push. Refer to realization of demo source code MiPush.

So far, MIUI push access has been completed. Now, push notification can be tested on MIUI devices.

Huawei Push Notification Service

Starting from newer versions of HMS, Huawei push uses an os-level connection scheme similar to that of Xiaomi push. But the service still depends on EMUI version and Huawei mobile service version. It's shown through massive tests that stable notification push requires EMUI4.1 and above and Huawei Mobile Service version 20502000 and above.

Steps to integrate Huawei push:

Prerequisites

  • Go to Official website of Huawei push, finish account registration and authentication, create an application and obtain AppID, AppKey, and AppSecret. For details, please refer to Huawei message push service.

  • Download Huawei Mobile Services (HMS) SDK 2.5.2.300. Download SDK and import the jar package into the project.

  • Go to CommsEase Admin Center and create a Huawei push certificate. Select the application requiring access to Xiaomi push and click Certificate Management:

Select "add certificate" in "Android push certificate", fill in the corresponding information in the pop-up box, where the application package name should be the package name of the developer's Android application, and the AppSecret should be the one obtained in the first step of creating application on the Huawei official website. please don’t forget the certificate name as it will be used when initiating the push with SDK.

Integration Procedure

Quick access to Huawei Push takes only 2 steps. For details, please refer to Huawei Push Access Document.

AndroidManifest.xml configuration, refer to manifest file configuration.

1. Set Huawei push appid

In AndroidManifest.xml, add the configured appid to Application node

xml<!-- Enter the appid of the application as configured by Huawei's push platform -->
<meta-data
    android:name="com.huawei.hms.client.appid"
    android:value="{Your Huawei push appid}" />

2. Add 2 receivers under Application node for receiving broadcast information

xml<!-- For third-party: Receive push message (registration, Push message, Push connection status) broadcast -->
<receiver android:name="com.netease.hwpushwrapper.PushBroadcastReceiver">
   <intent-filter>
       <!-- Required, used to receive token -->
       <action android:name="com.huawei.android.push.intent.REGISTRATION"/>
       <!-- Required, used to receive messages -->
       <action android:name="com.huawei.android.push.intent.RECEIVE"/>
       <!-- Optional, used to trigger the onEvent callback after clicking the notification bar or the button on it -->
       <action android:name="com.huawei.android.push.intent.CLICK"/>
       <!-- Optional, used to check whether the push channel is connected, ignore it if this check is not needed -->
       <action android:name="com.huawei.intent.action.PUSH_STATE"/>
   </intent-filter>
</receiver>
<receiver android:name="com.huawei.hms.support.api.push.PushEventReceiver" >
   <intent-filter>
       <!-- Receive the notification message from the channel, compatible with earlier versions of PUSH -->
       <action android:name="com.huawei.intent.action.PUSH" />
   </intent-filter>
</receiver>

3. Get token in Unity C# codes

Unity Demo provides an example of getting token of HUAWEI push:

Utils provides the interface InitHuaweiPush for connecting Huawei mobile service and getting token. The result will be notified to the object configured via the interface SetHuaweiPushReceiverObject. The following method is realized in MonoBehavior script of the object to get the result of connection and push token: (The character strings of following functions are presented in JSON format.)

Function Description Parameter
OnHuaweiPushConnected(string result) Connect push service successfully {"connection":bool}
OnHuaweiPushConnectionFailed(string result) Connect push service unsuccessfully {"connection":bool,"error":int}
OnHuaweiPushToken(string result) Get token result of HUAWEI push {"token":string,"belongId":string}
OnHuaweiPushMsg(string result) Receive transferred push message {"message",string}
OnHuaweiPushState(string result) Push status {"state",bool}

When the push token is acquired successfully, the function NIM.User.UserAPI.UpdatePushToken is invoked to submit certificate and token configured at CommsEase management background to CommsEase server. (Notes: The function can be invoked only after successful login.)

So far, Huawei push access has been completed and push notification now can be tested on Huawei devices that accommodate required conditions.

Unity Demo encapsulates some interfaces of pushing SDK with java codes. They are used in unity in the form of jar package. Java source codes are located in Demo Assets/Scripts/nim_push_java_src.rar, and users can update them as required and re-generate jar package to use in the program.

Upload Push Token

After completing the push configuration and obtaining the push token, upload the configuration information to the CommsEase server so that it can deliver the push message correctly.

  • API prototype
csharp/// <summary>
/// Update push certificate name and token
/// </summary>
/// <param name="certificateName">Certificate name configured at CommsEase management background</param>
/// <param name="token">Push token</param>
/// <param name="type">It is required for iOS only. 1 means pushkit and 0 means apns</param>
public static void UpdatePushToken(string certificateName,string token,int type)
Was this page helpful?
Yes
No
  • MIUI Push Notification Service
  • Prerequisites
  • Integration Procedure
  • Huawei Push Notification Service
  • Prerequisites
  • Integration Procedure
  • Upload Push Token