最新鸿蒙开发Dev5.0——通知

基础通知

  • 基础类型通知是一种简单的通知样式,用于显示重要的文本信息或简短的通知内容。它通常包含标题、内容和图标,用户可以通过点击通知来执行相关操作。这种通知适合用于提醒用户待办事项、设备状态、新消息等。

  • 类型枚举说明
    NOTIFICATION_CONTENT_BASIC_TEXT普通文本型
    NOTIFICATION_CONTENT_LONG_TEXT长文本文型
    NOTIFICATION_CONTENT_MULTILINE多行文本型
    NOTIFICATION_CONTENT_PICTURE图片型

  • 使用基础通知的一般步骤:

    • 导入notificationManager

    • import notificationManager from '@ohos.notificationManager'
    • 发布通知

    • ​
      // 构建通知结束
      let request: notificationManager.NotificationRequest = {
          id: 10,
          content:{
              //通知内容
          }
      }
      // 发布通知
      notificationManger.publish(request)
          .then( () => console.log('发送通知成功'))
          .catch( reason => console.log('发送通知失败', JSON.stringify(reason)))
      
      ​
    • 取消通知

    • //取消指定id的通知
      notificationMaager.cancel(10)
      //取消当前应用的所有通知
      notificationManager.cancelAll()

1. 普通文本型
let request: notificationManager.NotificationRequest = {
    id: 10,
    content:{
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal:{
            title:'通知标题',
            text: '通知内容详情',
            additionText: '通知附加内容'
        }
    },
    showDeliveryTime: true,
    deliveryTime: new Date().getTime(),
    groupName:  'wechat',
    notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION
}

groupName——分组功能 ,分为一个组的通知会集中显示

2. 长文本型
let request: notificationManager.NotificationRequest = {
    id: 10,
    content:{
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
        longText:{
            title:'通知标题',
            text: '通知内容详情',
            additionText: '通知附加内容',
            longTitle: '通知中的长文本,我很长,很长,很长。。。。。。。。。。。。。。。。。。。。。。。。。。',
            briefText: '通知概要和总结',
            expandedTitle: '通知展开时的标题'
        }
    }
}

3. 多行文本
let request: notify.NotificationRequest = {
      id: this.idx++,
      content:{
        notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_MULTILINE,
        multiLine:{
          title: '通知标题' + this.idx,
          text: '通知内容详情',
          additionalText: '通知附加内容',
          longTitle: '展开后的标题',
          briefText: '通知概要和总结',
          lines:[
            '第一行',
            '第二行',
            '第三行',
            '第四行',
          ]
        }
      },
      showDeliveryTime: true,
      deliveryTime: new Date().getTime(),
      notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION
    }
    this.publish(request)

4. 图片型
  • 将本地图标资源 转化为 PixelMap对象
    • 注意:不能直接使用 .png 等格式的图片资源
let request: notify.NotificationRequest = {
      id: this.idx++,
      content: {
        notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,
        picture: {
          title: '通知标题' + this.idx,
          text: '通知内容详情',
          additionalText: '通知附加内容',
          briefText: '通知概要和总结',
          expandedTitle: '展开后标题' + this.idx,
          picture: this.pixel
        }
      },
      showDeliveryTime: true,
      deliveryTime: new Date().getTime(),
      notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION
    }
    this.publish(request)
notificationSlotType类型
类型枚举说明状态栏图标提示音横幅
SOCIAL_COMMUNICATION社交类型√  √  √  
SERVICE_INFORMATION服务类型√  √  ×    
CONTENT_INFORMATION内容类型√  ×    ×    
OTHER_TYPES其他×    ×    ×    

进度条通知

  • 进度条通知会展示一个动态的进度条,主要用于文件下载,长任务处理的进度显示

    1. 判断当前系统是否支持进度条模板

      this.isSupport = await notificationManger.isSupportTemplate('downloadTemplate')
      if( !this.isSuport){
          return
      }

      NotificationTemplate属性

      名称类型只读可选说明
      namestring模板名称。当前仅支持'downloadTemplate':下载模板。
      dataRecord<string, Object>

      模板数据。

      - title: 表示下载标题。必填字段,值为字符串类型。

      - fileName: 表示下载文件名。必填字段,值为字符串类型。

      - progressValue: 表示下载进度,值为数值类型。

    2. 定义通知请求

    //3.1.准备进度条模板的参数
    let request: notify.NotificationRequest = {
      id: this.notificationId,
      template:{
        name: 'downloadTemplate',
        data: {
            title: '123',
            fileName: "123",
          progressValue: this.progressValue,
        }
      },
      wantAgent: this.wantAgentInstance,
      content: {
        notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: this.filename + ': ' + this.state,
          text: '',
          additionalText: this.progressValue + '%'
        }
      }
    }
    // 3.2通知请求
    let request: notificationManager.NotificationRequest = {
        id: 999
        template: template,
        content: {
            notification.ContentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
            normal: {
                title: this.filename + ': ' this.state,
                text: '123',
                additionalText: `${this.progressValue}%`,
            }
        }
        
    }

通知意图

  • 我们可以给通知或其中的按钮设置的行为意图(Want),从而实现拉起应用组件或发布公共事件等能力

    // 1.意图行为信息
    let wantInfo = {
        wants:[
            {
                devicedId: '',
                bundleName: 'com.example.myapplication',
                abilityName: 'EntryAbility',
                action: '',
                entities: []
            }
        ],
        operationType: wantAgent.OperationType.START_ABILITY //操作类型,启动应用,发布公共事件等
        requestCode: 0,
        wantAentFlags: [wantAgent.WantAgentFlags.CONSTRANT_FLAG] //标识行为意图的信息的
    }
    // 2.创建wantAgent实例
    this.wantAgentInstance = await wantAgent.getWantAgent(wantInfo)
    // 3.通知请求
    let request: notify.NotificationRequest = {
        id: 999,
        template: template,
        wantAgent: this.wantAgentInstance,
        content:{
        //...
        }
    }

  • 25
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值