Xiaomi Push Notifications
Chinese smartphones have occupied a large part smartphone market. To achieve longer battery support Chinese smartphones impose some restrictions on background processes. Due to these restrictions, many users are unable to receive push notifications.
To improve the notification delivery, Smartech has now added the support of the Xiaomi Push gateway. Smartech will send notifications through both Xiaomi push and FCM push gateway to increase the chances of delivery to the end-users. We ensure that the user will receive the given push notification only once.
Xiaomi Push Integration Steps
You need to perform a few steps to integrate Xiaomi Push in your app.
- Create a Xiaomi Developer account.
- Create an application on Xiaomi Push Console.
- To receive the push messages form Xiaomi, enable push from Xiaomi admin console.
- Download Xiaomi Push SDK.
- Follow Xiaomi Android SDK integration docs and integrate Xiaomi Push SDK into your project.
Setting Xiaomi Push Token/RegId
Similar to FCM, Xiaomi Push has Xiaomi Reg Id/Token which can be used to send push notifications to a device. To pass Xiaomi reg id/token to Smartech SDK use below snippet.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MiPushClient.registerPush(this, "XIAOMI_APP_ID", "XIAOMI_APP_KEY");
String xiaomiToken = MiPushClient.getRegId(this);
Smartech.getInstance(new WeakReference<>(context)).setXiaomiPushToken(context, xiaomiToken);
}
}
Getting Xiaomi Push RegId/Token
To retrieve the Xiaomi token set in the Smartech SDK use the below code.
Smartech.getInstance(new WeakReference<>(context)).getXiaomiPushToken(context);
Handle Xiaomi Push Notifications
Notifications sent via Xiaomi push are received in the custom BroadcastReceiver class which extends PushMessageReceiver of Xiaomi SDK. Make the below changes in the onReceivePassThroughMessage()
of your broadcast receiver. You don't need to trigger notification delivery and open event manually if you are using the above method.
public class XiaomiPushReceiver extends PushMessageReceiver {
@Override
public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
boolean isFromNetcore = Smartech.getInstance(new WeakReference<>(context)).handleXiaomiNotification(context, message.getContent());
if (!isFromNetcore) {
//Handle notification from other sources.
}
}
}
Send Notification Delivery Event
In case you don't want to use handleNotification()
and want to render Smartech notification on your own, Smartech recommends sending the delivery event. Use deliverXiaomiNotificationEvent()
method of NetcoreSDK to trigger delivery event.
public class XiaomiPushReceiver extends PushMessageReceiver {
@Override
public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
try {
boolean isFromNetcore = Smartech.getInstance(new WeakReference<>(context)).isNotificationFromNetcore(new JSONObject(message.getContent()));
if (isFromNetcore) {
JSONObject payload = new JSONObject(message.getContent());
Smartech.getInstance(new WeakReference<>(context)).renderNotification(context,payload); // Or you can use your own way to render notification.
Smartech.getInstance(new WeakReference<>(context)).deliverXiaomiNotificationEvent(context,payload); // Send delivery events if notification from Smartech.
} else {
//Handle notification from other sources.
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Send Notification Open/Click Event
To trigger notification open/click event for Smartech notifications, call the openXiaomiNotificationEvent()
of NetcoreSDK at the place where you are handling notification click. You need to pass the Json object of message content you received in the onReceivePassThroughMessage()
.
Smartech.getInstance(new WeakReference<>(context)).openXiaomiNotificationEvent(context,payload);
Note: Xiaomi Push Notifications support is available in AndroidX versions of Smartech SDK starting from 2.5.4.
Updated almost 4 years ago