HarmonyOS学习之路--通知篇

通知

基础通知

应用可以通过通知接口发送通知消息,提醒用户关注应用发生的变化.用户可以在通知栏中查看和操作通知内容.

实现通知:调用HarmonyOS提供的通知接口即可

1.导入notification模块

import notificationManager from '@ohos.notificationManager'

2.发布通知//2.1.构建通知请求

 let request: notificationManager.NotificationRequest ={ 
  id: 10,//通知的唯一标识,当id相同时,后面发的通知就会覆盖前面的
  content:{ // 通知内容:..}
  }

// 2.2.发布通知

 notificationManager.publish(request)
  .then(() => console.log('发送通知成功'))
  .catch(reason => console.log('发送通知失败’,JSON.stringify(reason)))

3.取消通知

默认情况,发出去的通知常驻在通知栏,除非用户手动删除,或者应用程序调用API清除:

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

不同功能的通知请求的request对象的结构

content的类型:

1.NOTIFICATION_CONTENT_BASIC_TEXT普通文本型,当通知内容过多时一行显示不下就会被隐藏

content:{ 
  contentType: notificationManager.ContentType. NOTIFICATION_CONTENT_BASIC_TEXT 
  normal : {
  title:'通知标题',
  text:'通知内容详情',
  additionalText:'通知附加内容'
  }
  }

2.NOTIFICATION_CONTENT_LONG_TEXT长文本型,一行显示不下会起到下一行

content:{ 
  contentType: notificationManager.ContentType. NOTIFICATION_CONTENT_LONG_TEXT 
  longText : {
  title:'通知标题',
  text:'通知内容详情',
  additionalText:'通知附加内容',
  longText:'通知中的长文本,我很长,我很长,我很长,', 
  briefText:'通知概要和总结',
  expandedTitle:'通知展开时的标题'
  }
  }
  }

3.NOTIFICATION_CONTENT_MULTILINE多行文本型,自带换行

content:{ 
  contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE 
  multiline: {
  title:'通知标题', 
  text:'通知内容详情',
  aiditionalText:'通知附加内容',
  briefText:'通知概要和总结',
  longitle:'展开时的标题,有多行,我很宽’, 
  lines:[
  ‘第一行',
  ‘第二行',
  '第三行',
  '第四行',
  ]
  }
  }
  }

4.NOTIFICATION_CONTENT_PICTURE图片型

 content:{ 
  contentType: notificationManager.ContentType. NOTIFICATION_CONTENT_PICTURE
  picture : {
  title:'通知标题',
  text:'通知内容详情', 
  briefText:'通知概要和总结',
  expandedTitle:'通知展开时的标题'
  picture:this.pixel//像素图
  }
  }
  }

注:图片型展示的是像素图.briefText只在图片型中起作用

像素图构建:指定图片地址把图片加载出来再把它创建成pixelMap:

 async aboutToAppear(){
  // 获取资源管理器
  let rm = getContext(this).resourceManager;
  //利用资源管理器读取图片
  let file = await rm.getMediaContent($r('app.media.watchGT4'))
  // 创建PixelMap
  image.createImageSource(file.buffer).createPixelMap()
  .then(value => this.pixel = value)
  .catch(reason => console.log('testTag','加载图片异常',JSON.stringify(reason)))

id和content只是request当中的两个重要字段而已,还有很多其他的,比如diliveryTime,showDiliveryTime,groupName,slotType

进度条通知

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

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

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

当当前系统支持进度条模板,this.isSupport为true时进行第二步

2.定义通知请求// 2.1.通知模板

   let template ={
  name: 'downloadTemplate', // 模板名称,必须是downloadTemplate
   data:{
  progressValue: this.progressValue,//进度条当前进度 
  progressMaxValue: 100 //进度条最大值
  }
  }

// 2.2.通知请求

和基础通知很像

 let request: notificationManager.NotificationRequest ={ 
  id: 999,
  template: template,
  content:{
  contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 
  normal: {
  title: this.filename + ':+ this.state, 
  text: '',
  additionalText:`${this.progressValue}%`,
  }
  }

从2.1.通知模板的progressValue: this.progressValue,//进度条当前进度 可知:进度条通知是静态的,想让他动态就不停的发通知,覆盖就行

通知行为意图

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

//1.定义意图行为信息

  
   let wantinfo ={
   wants: [
   {
  deviceid: '',
  bundleName: 'com.example.myapplication', 
  abilityName: 'EntryAbility', 
  action: ''
  entities: []
  }
  ],
  operationType: wantAgent.0perationType.START_ABILITY,//要做的事 
  requestCode: 0,
  wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]//标识这个行为意图的信息
  }

// 2.根据行为意图信息创建wantAgent实例

 this.wantAgentInstance = await wantAgent.getWantAgent(wantInfo)

// 3.通知请求

  
  let request: notify.NotificationRequest ={ 
  id: 999,
  template: template,//如果是进度条通知
  wantAgent: this.wantAgentInstance,// 设置通知意图 
  content: {
  }
  }

组件创建好就可以创建拉起当前应用的行为意图.即在aboutToAppear中定义意图行为信息,根据行为意图信息创建wantAgent实例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值