Last Updated on : 2024-01-10 09:09:17
This topic describes how to integrate Xiaomi push notifications with your Android app.
Before you configure the key information to enable Xiaomi push notifications, you must apply for the Mi Push channel. For more information, see Apply for Mi Push in Chinese. Log in to the Tuya IoT Development Platform, click the app that you want to manage on the SDK Development page, and then go to the Push Certificates tab of the app. Enter the key information for Mi Push in the Xiaomi Channel section.
The configurations must meet the following requirements:
tuya_common
, tuya_doorbell
, tuya_longbell
, and tuya_shortbell
notification categories are created in the Xiaomi background.Tuya Smart Life App SDK uses the regId
method provided by Mi Push to implement push notifications. You must integrate and initialize the SDK of Mi Push. For more information, see SDK Integration in Chinese.
Create the push channel to be used by Tuya Smart Life App SDK on Android Oreo. Example:
// The channel ID.
public static final String[] channelIds = {
"tuya_common",
"tuya_shortbell",
"tuya_longbell",
"tuya_doorbell"
};
// channel name
public static final String[] channelNames = {
context.getString(R.string.push_channel_common),
context.getString(R.string.push_channel_shortbell),
context.getString(R.string.push_channel_longbell),
context.getString(R.string.push_channel_doorbell)
};
// The custom sound files for the push channel.
public static final String[] channelSounds = {
"android.resource://" + context.getPackageName() + "/" + R.raw.tuya_common,
"android.resource://" + context.getPackageName() + "/" + R.raw.tuya_shortbell,
"android.resource://" + context.getPackageName() + "/" + R.raw.tuya_longbell,
"android.resource://" + context.getPackageName() + "/" + R.raw.tuya_doorbell
};
for (int i = 0; i < TuyaPushChnnels.channelIds.length; i++) {
createNotificationChannel(channelIds[i], channelNames[i], importance, soundPath);
}
@TargetApi(Build.VERSION_CODES.O)
private static void createNotificationChannel(String channelId, String channelName, int importance, String soundPath) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setSound(Uri.parse(soundPath), Notification.AUDIO_ATTRIBUTES_DEFAULT);
channel.setVibrationPattern(new long[]{
300, 1000, 300, 1000
});
channel.canBypassDnd();
channel.setBypassDnd(true);
channel.setLockscreenVisibility(VISIBILITY_SECRET);
NotificationManager notificationManager = (NotificationManager) MicroContext.getApplication().getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
regId
to cloudGet the register method from the class extends PushMessageReceiver
.
public static final String PUSH_PROVIDER_MI = "mi";
@Override
public void onCommandResult(Context context, MiPushCommandMessage message) {
L.i(TAG, "onCommandResult: " + message.toString());
String command = message.getCommand();
List<String> arguments = message.getCommandArguments();
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mRegId = cmdArg1;
Log.i(TAG, "mi push regid: " + mRegId);
if (!TextUtils.isEmpty(mRegId)) {
// Registers `regId` to the cloud and sends push notifications by `regId` from the cloud.
TuyaHomeSdk.getPushInstance().registerDevice(mRegId, PUSH_PROVIDER_MI, new IResultCallback() {
@Override
public void onError(String s, String s1) {
Log.e(TAG, "register device error: " + s + " " + s1);
}
@Override
public void onSuccess() {
Log.d(TAG, "register device success");
}
});
} else {
Log.e(TAG, "mRegId is empty!");
}
}
} else{
// TODO
}
}
The smart device triggers push notifications and the message will be displayed on the notification bar.
When the user taps the notifications on the notification bar, the custom notifications are returned by onNotificationMessageClicked
in extends PushMessageReceiver
of Xiaomi SDK. The key of the returned data is mi_link
.
@Override
public void onNotificationMessageClicked(Context context, MiPushMessage message) {
Log.i(TAG, "onNotificationMessageClicked: " + message.toString());
String message = message.getExtra().get("mi_link");
if (!TextUtils.isEmpty(message)) {
// TODO
Log.d(TAG, "mi_link: " + message);
}
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback