English
English
简体中文
Contact Us
Register
Log In

Integrate with Huawei Push Notifications

Last Updated on : 2022-02-07 10:42:41download

This topic describes how to integrate Huawei push notifications with your Android app.

Configure key information

Before you configure the key information to enable Huawei push notifications, you must apply for the Huawei Push Kit channel. For more information, see Apply for Huawei Push Kit 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 Huawei Push Kit in the Huawei Channel section.

Integrate with Huawei Push Notifications

The configurations must meet the following requirements:

  • The package name on the Huawei Developers platform is the same as the SDK app package name on the Tuya IoT Development Platform.
  • The correct SHA256 certificate fingerprint has been configured on the Huawei Developers platform.
Integrate with Huawei Push Notifications

Integrate with Huawei SDK

Tuya IoT App SDK uses the token method provided by Huawei Push Kit to implement push notifications. You must integrate and initialize the SDK of Huawei Push Kit. For more information, see SDK Integration in Chinese.

Create a channel

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);
}

Register pushToken to cloud

After Huawei SDK is integrated and initialized, you can get the push token and register the token to the cloud.

public static final String PUSH_PROVIDER_HUAWEI = "huawei";
String pushtoken = HmsInstanceId.getInstance(activity).getToken(appId, "HCM");
if (!TextUtils.isEmpty(pushtoken)) {
    Log.i(TAG, "get token:" + pushtoken);
    TuyaHomeSdk.getPushInstance().registerDevice(pushtoken, PUSH_PROVIDER_HUAWEI, new IResultCallback() {
        @Override
        public void onError(String code, String error) {
            Log.e(TAG, "registerDevice error: " + code + "   " + error);
        }

        @Override
        public void onSuccess() {
            Log.i(TAG, "register push token success");
        }
    });
}

Apply the following configurations to the activity of the splash page, such as the splash activity:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data
        android:host="Package name"
        android:path="/deeplink"
        android:scheme="@string/tuya_jump_scheme" />
</intent-filter>
  • Package name: the package name of your application.

  • tuya_jump_scheme: the content of the channel identifier.

Integrate with Huawei Push Notifications

The smart device triggers push notifications and the message will be displayed on the notification bar.

Parse custom data

When the user taps the notifications on the notification bar, the custom data is returned by intent of the activity that has intent-filter configured. The key of the returned data is hw_link.

Intent intent = getIntent();
if (intent != null) {
    String message = intent.getStringExtra("hw_link");
    if (!TextUtils.isEmpty(pushLink)) {
       String decodeMessage = new String(Base64.decodeBase64(message.getBytes()));
       // TODO
    }
}

FAQ

Why cannot push notifications be received?

  • Check whether the SHA256 certificate fingerprint has been configured as expected on the Huawei Developers platform and whether your application uses the same certificate fingerprint.
  • Use the Huawei Developers platform to send a message and check whether the message can be received.
  • For more information, see the documents of Huawei.