基础通知
应用可以通过通知接口发送通知消息,提醒用户关注应用中的变化。用户可以在通知栏查看和操作通知内容。
头文件:
import notificationManager from '@ohos.notificationManager'
发布通知:
取消通知:
通知内容
通知的重点在于通知内容。
let request:notificationManager.NotificationRequest={
id:10,
deliveryTime : new Date().getTime(), //获取当前通知发出的时间
showDeliveryTime : true, //显示出当前通知发出的时间
groupName : 'WeChat', //通知分组,同组的通知归一类
slotType : notify.SlotType.SOCIAL_COMMUNICATION, //通知展示的类型
content:{
// 通知内容
}
}
横幅即将通知显示在当前页面上。
通知内容的类型
下图依次对应上面四种类型的通知:
不同类型的通知代码
普通文本型
let request:notificationManager.NotificationRequest={
id:10,
content:{
// 通知内容
contentType : notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT
normal : {
title:'通知标题',
text:'通知内容详情',
additionalText:'通知的附加内容'
}
}
}
长文本型
let request:notificationManager.NotificationRequest={
id:10,
content:{
// 通知内容
contentType : notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT
longText : {
title:'通知标题',
text:'通知内容详情',
additionalText:'通知的附加内容',
//点击下拉展开后展示的内容
longText : '通知中的长文本',
briefText : '通知的总结和概要',
expandedTitle : '通知展开时的标题'
}
}
}
多行文本型
let request:notificationManager.NotificationRequest={
id:10,
content:{
// 通知内容
contentType : notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE
multiline : {
title:'通知标题',
text:'通知内容详情',
additionalText:'通知的附加内容',
//点击下拉展开后展示的内容
briefText : '通知的总结和概要',
longTitle : '通知展开时的标题',
lines : [
'第一行',
'第二行',
'第三行',
'第四行',
]
}
}
}
图片型
let request:notificationManager.NotificationRequest={
id:10,
content:{
// 通知内容
contentType : notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE
picture : {
title:'通知标题',
text:'通知内容详情',
additionalText:'通知的附加内容',
//点击下拉展开后展示的内容
briefText : '通知的总结和概要',
expandedTitle : '通知展开时的标题',
picture : this.pixel //该图片的读取只能是按像素类型读取
}
}
}
图片按像素类型读取:
进度条通知
进度条通知会展示一个动态的进度条,主要用于文件下载,长任务处理的进度显示。
判断当前模块是否支持进度条模板:
let isSuport = await notificationManager.isSupportTemplate('downloadTemplate')
//上面是一个异步函数
if(!isSuport)
{
return
}
定义通知请求:
发布通知与上面基础通知方法一样,进度条通知是静态通知,为了让通知进度条与组件进度条一致,需要不断的发送通知。
通知意图
我们可以给通知或其中按钮设置行为意图(Want),从而实现拉起应用组件或发布公共事件等能力,即用户可以通过通知来对应用进行一些操作。
operationType 关键字—标识该通知意图的操作类型,上图的类型是启动Wants参数中对应的ability。
request 关键字—自定义的返回码。
wantAgentFlags 关键字—标识该通知意图的类型,上图表示是该通知意图是常量。