集成小米推送

更新时间:2023-11-10 02:26:15下载pdf

本文介绍如何为 Android App 集成小米平台的消息推送。

配置密钥

申请小米推送的方式,请参考 申请小米推送。您需要将申请到的 AppID、AppKey 等信息复制到 涂鸦 IoT 平台 中对应应用的配置中。

集成小米推送

请勿将信息配置错误

  • 确认小米后台包名与 SDK 应用包名一致。
  • 确认在小米后台 通知类别 中要创建 tuya_commontuya_doorbelltuya_longbelltuya_shortbell 通道。
集成小米推送

集成小米 SDK

智能生活 App SDK 使用小米推送的 regId 方式进行推送。小米推送的 SDK 接入、初始化请按照 小米官方文档 进行接入,并完成《SDK 接入指南》部分。

创建 Channel

参考如下代码,在 Android O 以上创建涂鸦使用的推送 Channel。

// 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)
};
// 通知渠道的自定义声音文件
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 <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 到涂鸦

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)) {
                // 注册 regId 到涂鸦,涂鸦使用注册的 regId 进行推送
                ThingHomeSdk.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
    }
}

完成后,请在设备侧等触发消息推送,手机通知栏会有相应的推送显示。

解析自定义数据

当用户点击通知栏的通知后,您可以在小米 extends PushMessageReceiveronNotificationMessageClicked 中获取自定义消息,获取数据的 Key 值 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);
  }
}