Account-related APIs
This topic describes how to use the HarmonyOS SDK to attach accounts to and detach accounts from a device.
Description
You can use the Mobile Push SDK to attach accounts to and detach accounts from a device. After you attach an account, you can push messages to that account. Any device attached to the account receives the messages.
Attach an account
You can attach an in-app account to a push channel to send targeted messages to that account.
An account name can be up to 64 bytes in length.
A device can be attached to only one account. An account can be attached to multiple devices.
To change the account attached to a device, call the attach method directly. The new account automatically overwrites the old one. You do not need to detach the old account first. This prevents potential concurrency issues caused by frequent operations.
The SDK provides the bindAccount method to attach an account. The following code shows an example:
import { aliyunPush } from '@aliyun/push';
aliyunPush.bindAccount(`ExampleAccount`, (err) => {
if (err) {
console.error(`Failed to attach the account. Error code: ${err.code}. Error message: ${err.message}`);
return;
}
console.info(`Account attached successfully.`);
})
Detach an account
The SDK provides the unbindAccount method to detach an account. The following code shows an example:
import { aliyunPush } from '@aliyun/push';
aliyunPush.unbindAccount((err) => {
if (err) {
console.error(`Failed to detach the account. Error code: ${err.code}. Error message: ${err.message}`);
return;
}
console.info(`Account detached successfully.`);
})