通知
基础通知
应用可以通过通知接口发送通知消息,提醒用户关注应用发生的变化.用户可以在通知栏中查看和操作通知内容.
实现通知:调用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实例