目录

Adding a Custom Ringtone to a Notification

Starting from API version 12, applications can use custom notification ringtones when publishing notifications. The audio resources for these ringtones must be pre-included in the application. Starting from API version 24, the system has enhanced this field, allowing applications to use non-preloaded audio resources as notification ringtones, such as audio downloaded from the internet or user-generated audio files.

API Description

Customized ringtone can be specified through the sound field carried by NotificationRequest. If this field is not specified, the system ringtone is used by default.

Supported formats: m4a, aac, mp3, ogg, wav, flac, amr, ...

Table 1 API for publishing notifications

API Description
publish(request: NotificationRequest): Promise<void> Publishes a notification.

How to Develop

  1. Import modules.

    import { notificationManager } from '@kit.NotificationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { contextConstant, common } from '@kit.AbilityKit';
    import { fileIo as fs, fileUri } from '@kit.CoreFileKit';
    
    const TAG: string = '[SpecifiedCustomizedRingtone]';
    const DOMAIN_NUMBER: number = 0xFF00;
    const SOUND_FILE_NAME: string = 'ringtone_demo.mp3'; // Replace with your actual audio file.
  2. Prepare audio resources.

    Scenario 1: Use audio resources preloaded in the application.

    (1) Place the audio resources in the resources/rawfile directory of the project.

    (2) Create the sound information for the notification.

    let soundFile: string = SOUND_FILE_NAME; // Replace it with the corresponding audio file in the resources/rawfile directory.

    Scenario 2: Use audio resources in the sandbox.

    (1) Generate the sandbox audio resource path.

    // Generate the sandbox audio resource path.
    const uiContext: UIContext = this.getUIContext();
    let context: Context | undefined = uiContext.getHostContext() as common.UIAbilityContext;
    if (!context) {
      hilog.error(DOMAIN_NUMBER, TAG, 'Context is not initialized.');
      return;
    }
    const applicationContext: common.ApplicationContext = context.getApplicationContext();
    applicationContext.area = contextConstant.AreaMode.EL1; // Must be in the EL1 sandbox.
    const sandboxDir: string = applicationContext.filesDir;
    let sandboxFilePath: string = sandboxDir + '/' + SOUND_FILE_NAME;

    (2) Place internet-downloaded or user-generated audio resources in the files directory or its subdirectories in the EL1 area of the sandbox directory. The example below copies audio from resources/rawfile to the specified sandbox directory.

    // Copy the audio file in the resources/rawfile/ directory to the files directory of the application sandbox EL1.
    try {
      let rawFileData: Uint8Array = context.resourceManager.getRawFileContentSync(SOUND_FILE_NAME);
      if (!fs.accessSync(sandboxDir)) {
        fs.mkdirSync(sandboxDir, true);
      }
      const file = fs.openSync(sandboxFilePath, fs.OpenMode.CREATE | fs.OpenMode.WRITE_ONLY);
      fs.writeSync(file.fd, rawFileData.buffer);
      fs.closeSync(file);
      hilog.info(DOMAIN_NUMBER, TAG, 'copy file to sandbox success.');
    } catch (error) {
      hilog.error(DOMAIN_NUMBER, TAG, `copy file to sandbox error: ${JSON.stringify(error)}`);
      return;
    }

    (3) Create the sound information for the notification.

    // Obtain the URI of the sandbox file.
    let sandboxFileUri: string = fileUri.getUriFromPath(sandboxFilePath)
    let soundFile: string = 'uri::' + sandboxFileUri; // Must start with uri::, and the path must not contain '../' or '/..'.
  3. Publish a notification that carries the customized ringtone.

    let notificationRequest: notificationManager.NotificationRequest = {
      id: 0,
      notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: 'title',
          text: 'text',
          additionalText: 'test_additionalText'
        }
      },
      sound: soundFile
    }
    
    notificationManager.publish(notificationRequest).then(() => {
      hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
    }).catch((err: BusinessError) => {
      hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
    });

Constraints