After selecting a service notification template for your atomic service, you can proactively initiate a subscription request to a user who has signed in using their HUAWEI ID. Push Kit will then display a pop-up asking for user authorization. After the user selects an option in the pop-up, Push Kit returns the selection to your atomic service, so you can process the message delivery logic accordingly.
A single atomic service can initiate up to 30 subscription requests every 5 minutes to the same device. When the upper limit is reached, frequency control takes effect and the atomic service will not be able to call the subscription API. The count is reset after 5 minutes.
The pop-up of a one-time subscription notification includes the following information:
The pop-up needs to provide the following buttons for the subscription:
After a user selects the option Always allow, no pop-up will be displayed when you call serviceNotification.requestSubscribeNotification() again for the same selected templates and the previous selection will be retained. To modify the selection, users need to access the settings page of the atomic service.
The pop-up of a long-term subscription notification includes the following information:
The pop-up needs to provide the following buttons for the subscription:
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { serviceNotification } from '@kit.PushKit';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the page. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
}
async requestSubscribeNotification() {
try {
// Replace entityIds with the IDs of the templates to be subscribed to.
let entityIds: string[] = ['13B6000E24008000', '13B90C82D3802680'];
let type: serviceNotification.SubscribeNotificationType =
serviceNotification.SubscribeNotificationType.SUBSCRIBE_WITH_HUAWEI_ID;
const res: serviceNotification.RequestResult =
await serviceNotification.requestSubscribeNotification(this.context, entityIds, type);
hilog.info(0x0000, 'testTag', 'Succeeded in requesting serviceNotification: %{public}s',
JSON.stringify(res.entityResult));
} catch (err) {
let e: BusinessError = err as BusinessError;
hilog.error(0x0000, 'testTag', 'Failed to request serviceNotification: %{public}d %{public}s', e.code, e.message);
}
}
async onForeground(): Promise<void> {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
try {
// Call the method after the page loading is complete and UIAbilityContext has been obtained.
await this.requestSubscribeNotification();
} catch (err) {
let e: BusinessError = err as BusinessError;
hilog.error(0x0000, 'testTag', 'Request subscribe notification failed: %{public}d %{public}s', e.code, e.message);
}
}
}
ACCEPTED: The user agrees to subscribe to the notification template of the specified ID.
REJECTED: The user rejects to subscribe to the notification template of the specified ID.
FILTERED: The template is filtered out by the background due to duplicate template titles.
BANNED: The template is banned by the background.
For details, please refer to RequestResult.
You can complete development by referring to the sample code for account-based service notification subscription initiation.