Xiaomi Push Integration

Create Xiaomi Account

Kindly follow the below steps to create your Xiaomi account:

Step 1: Register as a Xiaomi developer by logging onto the Xiaomi Developer Account.

Step 2: Click on Create App and enter the required details.

Step 3: After the App is created, click on the App Name to find the *Package Name and App Secret and configure the same on your dashboard by following the steps mentioned in this

Integrating Xiaomi Push SDK in Android Studio Project

Step 1: Login to your Xiaomi Account and download the Xiaomi Android SDK (v4.8.3).
and above).

Step 2: Integrate the MiPush_SDK_Client_4_8_3.jar file into your Android Studio Project (As previously done in the PushDemo4Studio demo project).

Step 3: Add the following code inside the AndroidManifest.xml file

<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.VIBRATE"/>
<!-- the following 2 yourpackage should be changed to your
package name -->
<permission
android:name="yourpackage.permission.MIPUSH_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="yourpackage.permission.
MIPUSH_RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE"
/>
<service
android:enabled="true"
android:process=":pushservice"
android:name="com.xiaomi.push.service.XMPushService"/>
<service
android:name="com.xiaomi.push.service.XMJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":pushservice" />
<!--NoteThis service must be added to the version 3.0.1 or
laterincluding version 3.0.1-->
<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" />
<!--Notethis service must be added to version 2.2.5 or later
includes version 2.2.5-->
<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=":pushservice"
android:name="com.xiaomi.push.service.receivers.
PingReceiver" >
<intent-filter>
<action android:name="com.xiaomi.push.PING_TIMER" />
</intent-filter>
</receiver>
  • To receive messages, kindly implement a BroadcastReceiver inherited from PushMessageReceiver and realize all required methods in it: onReceivePassThroughMessage, onCommansResult, and then register your receiver in the AndroidManifest.xml file. Method onReceivePassThroughMessage is used to receive transparent messages sent by the server.

  • To pass the Push Token to WebEngage, kindly add the following code inside the MIPushReceiver onCommandResult method.

  • To pass the received push payload to WebEngage SDK, add the following code

Example of BroadcastReceiver: MIPushReceiver

public class MIPushReceiver extends PushMessageReceiver {
@Override
public void onReceivePassThroughMessage(Context context,
MiPushMessage miPushMessage) {
super.onReceivePassThroughMessage(context,
miPushMessage);
String content = miPushMessage.getContent();
WebEngage.get().receive(PushUtils.prepareMap(content));
}
@Override
public void onCommandResult(Context context,
MiPushCommandMessage message) {
super.onCommandResult(context, message);
String command = message.getCommand();
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
WebEngage.get().setXiaomiRegistrationID
(MiPushClient.getRegId(context));
}
}
}
}
  • In the application file, kindly add the following code to initialize MI Code.
MiPushClient.registerPush(this, APP_ID, APP_KEY);

Configure Xiaomi Push creds on Dashboard

Kindly follow the steps mentioned in this doc to configure the Xiaomi creds on your Dashboard.

👍

Congratulations!

You can now successfully send push notifications using Mi push service.


Did this page help you?