Alias-related APIs

更新时间:
复制 MD 格式

This topic describes how to use the HarmonyOS SDK to add and remove aliases for a device.

Feature description

The Mobile Push SDK lets you add and remove aliases for devices. After you add an alias, you can send push notifications by alias. Devices associated with the alias receive the push notifications.

Add an alias

The SDK provides the addAlias method to add an alias. The following code shows an example:

import { aliyunPush } from '@aliyun/push';

aliyunPush.addAlias(`ExampleAlias`, (err) => {
  if (err) {
    console.error(`Failed to add alias. Error code: ${err.code}. Error message: ${err.message}`);
    return;
  }
  console.info(`Alias added successfully.`);
})

Remove an alias

The SDK provides the removeAlias method to remove an alias. The following code shows an example:

import { aliyunPush } from '@aliyun/push';

aliyunPush.removeAlias(`ExampleAlias`, (err) => {
  if (err) {
    console.error(`Failed to remove alias. Error code: ${err.code}. Error message: ${err.message}`);
    return;
  }
  console.info(`Alias removed successfully.`);
})

Query aliases

The SDK provides the listAliases method to query the aliases of a device. The following code shows an example:

import { aliyunPush } from '@aliyun/push';

aliyunPush.listAliases((err, aliases) => {
  if (err) {
    console.error(`Failed to query aliases. Error code: ${err.code}. Error message: ${err.message}`);
    return;
  }
  console.info(`Aliases queried successfully: ${aliases?.join(',')}`);
})