目录

Initiating an Account-based Subscription Request

Use Case

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.

Frequency Control

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.

Pop-up Effect

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:

How to Develop

  1. Ensure that you have completed necessary preparations for development as well as enabled service notifications and selected required subscription templates.
  2. Call the serviceNotification.requestSubscribeNotification() method to initiate message subscription. The following is an example:

    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);
        }
      }
    }
    • entityIds: IDs of the subscription templates. Use the template IDs obtained under Atomic Service Notification. Up to three templates can be subscribed to at a time.
      • A subscription request cannot contain IDs of templates that use the same template title. Otherwise, only the template of the first ID is retained in the subscription pop-up, and the subscription result of the template ID that is filtered out is FILTERED.
      • A subscription request cannot contain both one-time and long-term subscription notification template IDs. Otherwise, the subscription pop-up will fail to be displayed.
    • type: subscription type. Atomic services support only account-based subscriptions. Set this parameter to SUBSCRIBE_WITH_HUAWEI_ID. For details, please refer to SubscribeNotificationType.
    • res: subscription result for the template with the specified ID. The value can be ACCEPTED, REJECTED, FILTERED, BANNED, or UNKNOWN.

      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.

  3. After the user agrees to subscription (ResultCode is set to 0), push the message to the specific HUAWEI ID. For details, please refer to Pushing Account-based Subscription Notifications.

Sample Code

You can complete development by referring to the sample code for account-based service notification subscription initiation.