HarmonyOS 鸿蒙Next Notification Kit创建通知栏的几个问题

HarmonyOS 鸿蒙Next Notification Kit创建通知栏的几个问题

通过Notification Kit创建自定义通知栏消息,有以下几个问题:

1、已经给了APP所有推送的权限,但是没有震动和铃声

2、推送的消息隐藏在通知栏没有自动显示,需要下拉才能显示。

2 回复

尝试这个demo呢:


import { notificationManager } from '@kit.NotificationKit';
import Base from '@ohos.base';
import wantAgent from '@ohos.app.ability.wantAgent';
import { WantAgent } from '@ohos.app.ability.wantAgent';
// 通过WantAgentInfo的operationType设置动作类型
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @StorageLink('test') test: object = new Object;
  publishNotification() {
    notificationManager.requestEnableNotification().then(() => {
      let wantAgentInfo: wantAgent.WantAgentInfo = {
        wants: [
          {
            deviceId: '',
            bundleName: 'com.example.testnotice',
            abilityName: 'EntryAbility',
            action: '',
            entities: [],
            uri: '',
            parameters: {
              title: 'xxxx'
            }
          }
        ],
        operationType: wantAgent.OperationType.START_ABILITY,
        requestCode: 0,
        wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
      };
      wantAgent.getWantAgent(wantAgentInfo).then((data: WantAgent) => {
        let notificationRequest: notificationManager.NotificationRequest = {
          id: 1,
          content: {
            notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
            normal: {
              title: '加入购物车成功',
              text: '已加入购物车',
            }
          },
          notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
          wantAgent: data,
        }
        notificationManager.publish(notificationRequest).then(() => {
          console.info('publish success');
        }).catch((err: Error) => {
          console.error(`publish failed,message is ${err}`);
        });
      })
    }).catch((err: Base.BusinessError) => {
      console.error(`requestEnableNotification fail: ${JSON.stringify(err)}`);
    });
  }
  build() {
    Column() {
      Text('test')
        .fontSize(50)
        .onClick(() => {
          console.info('test value:' + this.test);
        })
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.publishNotification();
        })
    }
    .height('100%')
    .width('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next Notification Kit创建通知栏的几个问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对您提到的HarmonyOS 鸿蒙Next Notification Kit创建通知栏的几个问题,以下进行简要回答:

  1. 通知权限: 确保应用已获得通知权限。在鸿蒙系统中,用户需要在系统设置中手动为应用开启通知权限。

  2. 通知内容: 创建通知时,需确保内容符合系统规定,避免使用敏感词汇或格式错误。同时,通知标题和内容应简洁明了,以提高用户体验。

  3. 通知类型: 根据需求选择合适的通知类型,如普通通知、悬浮通知等。不同类型的通知在展示方式和用户交互上有所不同。

  4. 通知图标: 确保通知图标清晰、易识别,且与应用主题相符。图标尺寸需符合鸿蒙系统的规范,以保证在通知栏中的显示效果。

  5. 通知点击事件: 设置通知点击事件时,需确保事件处理逻辑正确,且与应用的其他功能相协调。避免点击通知后出现无响应或错误跳转的情况。

  6. 通知更新与删除: 在需要更新或删除通知时,需使用正确的API接口,并提供正确的通知ID。确保通知能够及时更新或删除,避免给用户造成困扰。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。

回到顶部