This topic explains how to use the HarmonyOS SDK to bind tags to and unbind tags from a device.
Overview
The Mobile Push SDK lets you bind tags to and unbind tags from devices. After a tag is bound, you can send notifications to that tag. All devices bound to the tag will receive the notification.
Bind tags
Use the bindTag method to bind tags.
The following code binds tags to a device:
import { aliyunPush, TAG_TARGET } from '@aliyun/push';
aliyunPush.bindTag({
target: TAG_TARGET.DEVICE,
tags: ['SampleTag1', 'SampleTag2']
}, (err) => {
if (err) {
console.error(`Failed to bind tags to the device. Error code: ${err.code}, Error message: ${err.message}`);
return;
}
console.info(`Tags are successfully bound to the device.`);
})Unbind tags
Use the unbindTag method to unbind tags.
The following code unbinds tags from a device:
import { aliyunPush, TAG_TARGET } from '@aliyun/push';
aliyunPush.unbindTag({
target: TAG_TARGET.DEVICE,
tags: ['SampleTag1', 'SampleTag2']
}, (err) => {
if (err) {
console.error(`Failed to unbind tags from the device. Error code: ${err.code}, Error message: ${err.message}`);
return;
}
console.info(`Tags are successfully unbound from the device.`);
})List tags
Use the listTags method to list the tags bound to a device.
The following code lists the tags for a device:
import { aliyunPush } from '@aliyun/push';
aliyunPush.listTags((err, tags) => {
if (err) {
console.error(`Failed to list device tags. Error code: ${err.code}, Error message: ${err.message}`);
return;
}
console.info(`Device tags listed successfully: ${tags?.join(',')}`);
})该文章对您有帮助吗?