HarmonyOS notificationManager普通文本通知点击事件,和点击通知后自动消失

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

参考demo:

import Notification from '@ohos.notificationManager';
import wantAgent from '@ohos.app.ability.wantAgent';
import { WantAgent } from '@ohos.app.ability.wantAgent';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct InputPage {
  @State message: string = 'Hello World';

  notification(message:string) {
    let wantAgentObj:WantAgent | undefined = undefined// 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
    // 通过WantAgentInfo的operationType设置动作类型
    let wantAgentInfo:wantAgent.WantAgentInfo = {
      wants: [
        {
          deviceId: '',
          bundleName: 'com.example.application',
          abilityName: 'EntryAbility',
          action: '',
          entities: [],
          uri: '',
          parameters: {}
        }
      ],
      operationType: wantAgent.OperationType.START_ABILITY,
      requestCode: 0,
      wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
    };

    // 创建WantAgent
    wantAgent.getWantAgent(wantAgentInfo, (err:BusinessError, data:WantAgent) => {
      if (err) {
        console.error(`Failed to get want agent. Code is ${err.code}, message is ${err.message}`);
        return;
      }
      console.info('Succeeded in getting want agent.');
      wantAgentObj = data;
      let notificationRequest: Notification.NotificationRequest = {
        id: 1,
        content: {
          notificationContentType:Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
          normal: {
            title: "通知测试",
            text: message,
          }
        },
        notificationSlotType:Notification.SlotType.SOCIAL_COMMUNICATION,
        wantAgent: wantAgentObj
      }

      Notification.requestEnableNotification().then(() => {
        Notification.publish(notificationRequest).then(() =>{
          console.log("发布成功")
        }).catch((err: BusinessError) => {
          console.error(`publish:${err}`)
        })
      }).catch((err: BusinessError) => {
        console.error(`${err}`)
      })
    });
  }

  build() {
    Column(){
      Button('发布通知').onClick(() => {
        this.notification("这是一个通知")
      })
    }
  }
}

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/notification-with-wantagent-V5#ZH-CN_TOPIC_0000001893210605__%E5%BC%80%E5%8F%91%E6%AD%A5%E9%AA%A4

分享
微博
QQ
微信
回复
3天前
相关问题
通知点击事件监听的方法
447浏览 • 1回复 待解决
HarmonyOS文本点击事件
508浏览 • 1回复 待解决
HarmonyOS 自定义通知点击更新需求
43浏览 • 1回复 待解决
如何做到点击通知消息打开应用
540浏览 • 1回复 待解决
HarmonyOS 通知列表刷新事件
75浏览 • 1回复 待解决
怎么给通知加单击事件?
4925浏览 • 1回复 待解决
HarmonyOS点击文本实现问题
26浏览 • 1回复 待解决
HarmonyOS 卡片点击事件失效
22浏览 • 1回复 待解决