Harmony OS 用户通知服务

用户通知服务

简介

  • 将应用产生的通知直接在客户端本地推送给用户,本地通知根据通知类型及发布场景会产生对应的铃声、震动、横幅、锁屏、息屏、通知栏提醒和显示。

能力

  • 文本通知
    • 纯文本通知( 短文本类型(单行文本), 长文本类型(多行文本) )
    • 图文通知
  • 进度条通知
  • 特殊的通知( 角标 )
  • 过程
    • 当前用用需要获取用户授权, 才能发送通知
    • 授权成功才可以正常发送通知
    • 给了我们几个 API
      => 查看是否有授权的 API, isNotificationEnabled()
      => 发送通知的方法 requestEnableNotification()
      -> 向用户请求发送通知, 第一次请求的时候会弹窗

做消息通知之前首先要了解一下通知类型和通道类型(通知方式:app小圆点、状态栏图标、横幅、提示音)

1.通知类型

  • 实际上就是设置通知卡片的类型
  • 设置手机下拉任务栏界面,通知卡片的样式和内容会有所不同
ContentType说明
NOTIFICATION_CONTENT_BASIC_TEXT0普通类型通知
NOTIFICATION_CONTENT_LONG_TEXT1长文本类型通知
NOTIFICATION_CONTENT_PICTURE2图片类型通知
NOTIFICATION_CONTENT_CONVERSATION3社交类型通知。预留能力,暂不支持
NOTIFICATION_CONTENT_MULTILINE4多行文本
NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW5实况窗类型通知。不支持三方应用直接创建该类型通知,可以由系统代理创建系统实况窗类型通知后,三方应用发布同ID的通知来更新指定内容。
NOTIFICATION_CONTENT_LIVE_VIEW6普通实况窗类型通知。只支持系统应用

2.通道类型(通知方式:app小圆点、状态栏图标、横幅、提示音)

  • 加*的为常用类型
SlotTypeSlotTypeValSlotType说明SlotLevel
UNKNOWN_TYPE0未知类型LEVEL_MIN
SOCIAL_COMMUNICATION*1社交通信LEVEL_HIGH
SERVICE_INFORMATION*2服务提醒LEVEL_DEFAULT
CONTENT_INFORMATION*3内容资讯LEVEL_MIN
LIVE_VIEW4实况窗,不支持三方应用直接创建该渠道类型通知,可以由系统代理创建后,三方应用发布同ID的通知来更新指定内容。LEVEL_DEFAULT
CUSTOMER_SERVICE5客服消息。该类型用于用户与商家之间的客服消息,需由用户主动发起。LEVEL_LOW
OTHER_TYPES*0xFFFF其他LEVEL_MIN
slotLevel:通知优先级,优先级越高
SlotLevelSlotLevelValSlotLevel说明
LEVEL_NONE0表示关闭通知功能
LEVEL_MIN11.状态栏中不显示通知图标 2.没有横幅 3.没有提示音
LEVEL_LOW21.状态栏中显示通知图标 2.没有横幅 3.没有提示音
LEVEL_DEFAULT31.状态栏中显示通知图标 2.没有横幅 3.有提示音
LEVEL_HIGH41.状态栏中显示通知图标 2.有横幅 3.有提示音

3.请求通知授权

  • 1.通过固定api(notificationManager.isNotificationEnabled)查询是否已授权
  • 2.若未授权,通过固定api(notificationManager.requestEnableNotification)请求用户授权
  • 3.授权成功后,才能发送通知
// 导入依赖 
import { abilityAccessCtrl, common } from '@kit.AbilityKit';

// 定义一个方法
isEnabled(callback:()=>void){
  // 调用api 查询是否已授权
  notificationManager.isNotificationEnabled()
    .then((res)=>{
      console.log('用户授权 => ',JSON.stringify(res));
      if(!res){
        // 没有授权,调用api 请求用户授权
        notificationManager.requestEnableNotification(this.context)
          .then(()=>{
            console.log('发送通知1 => ');
            callback()
          })
          .catch(()=>{
            console.log('不允许发送通知 => ');
            const temp = abilityAccessCtrl.createAtManager()
            temp.requestPermissionsFromUser(this.context, ['ohos.permission.NOTIFICATION_CONTROLLER'])
          })
      } else {
      // 已授权
        console.log('发送通知2 => ');
        callback()
      }

    })
    .catch(()=>{
      console.log('=> 查询失败')
    })
  }

4.发送通知–在获取权限的情况下

  • 1.导入模块
  import { notificationManager } from '@kit.NotificationKit';
  • 2.首先创建 NotificationRequest 对象, 官网文档

  • 文本通知

    • => 通知类型根据实际业务改变 notificationContentType
    • => 通知方式根据实际业务改变notificationSlotType
    • => 根据实际业务设置content中文本
      • 当前通知配置( 不同类型对应的 key 不一样 )
      • 普通文本 => normal
      • 长文本 => longText
      • 多行文本 => multiLine
const notificationInfo: notificationManager.NotificationRequest = {
  // id 必填: 表示当前通知的序号( 如果一样, 替换当前, 如果不一样, 就增加一个 )
  id: ++this.count,
  // 当前通知内容
  content: {
    // 当前通知类型
    notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,

    normal: {
      text: '通知的内容' + this.count,
      title: '通知的标题' + this.count,
    }
  },
  // 通知的类型
  notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
  // 通知的小分组
  groupName: 'Aaa'
}
  • 3.发送通知
 notificationManager.publish(notificationInfo)

5.进度条通知

  • 进度条通知需要一个模板
  • 其他方式都和普通通知一样
// 进度
sendSchedule(){
  // 创建一个变量缓存进度
  this.downloadCount += 1
  //   0.创建模板
  const template:notificationManager.NotificationTemplate = {
    name: 'downloadTemplate', 
    data:{
      title: '下载内容',    // 标题
      fileName: '下载完成', // 文本内容
      progressValue: this.downloadCount * 10,    // 当前进度
      progressMaxValue: 100, // 最大值
    }
  }
  // 1. 准备通知对应的信息对象
  const notificationInfo:notificationManager.NotificationRequest = {
    // 做进度通知,这个id需要写死,这样更新进度就会覆盖掉之前的通知
    id:this.count,
    content:{
      notificationContentType:notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
      normal: {
        text: '正在下载文件',
        title: '下载文件',
        // additionalText:  80 + '%'
      }
    },
    template,
    // 通知的类型
  }

  // 2. 把通知发送出去( 按说应该是根据某些逻辑发送通知 )
  notificationManager.publish(notificationInfo, (err) => {
    // 当完成的时候, 会触发当前回调函数
    // 如果有错误, err 有内容
    // 如果没有错误, 那么 err 没有内容
    console.log('wuwuwu => 通知发送成功')
  })
}

6.点击通知调起ability

  • 首先需要创建want对象
  • 然后就还是正常发通知的步骤
// 案例使用 长文本通知

// 拉起ability
pullUpAbility() {
  // 1.创建一个 want 信息 对象
  const wantAgentInfo: wantAgent.WantAgentInfo = {
    wants: [
      {
        bundleName: 'com.example.testnitification',
        abilityName: 'EntryAbility',
        parameters: {  // 在 EntryAbility 的 want 中解析参数
          txt: 'aaasssaa'
        }
      }
    ],
    operationType: wantAgent.OperationType.START_ABILITY,
    requestCode: 0,
    wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
  }

  // 2.创建 WantAgent
  wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
    if (err) {
      console.log('wuwuwu 创建 WantAgent 出错 =>');
      return
    }

    //   3.准备通知对应的信息对象
    const notificationInfo: notificationManager.NotificationRequest = {
      id: ++this.count,
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
        longText: {
          title: '长文本title',
          text: '长文本text',
          briefText: '长文本briefText',
          longText: 'longText',
          expandedTitle: 'expandedTitle'
        }
      },
      notificationSlotType: notificationManager.SlotType.CUSTOMER_SERVICE,
      wantAgent: data
    }

    //   4.发送通知
    notificationManager.publish(notificationInfo, () => {
      console.log('wuwuwu 发送通知 => ');
    })
  })
}

// 解析参数---EntryAbility中生命周期
// 参数都存在 want.parameters 中
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  if (want?.parameters) {
    console.log('wuwuwu onCreate => ', JSON.stringify(want.parameters));
  }
}

onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  if (want?.parameters) {
    console.log('wuwuwu onNewWant => ', JSON.stringify(want.parameters));
  }
}
  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值