基础通知

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

  1. 导入notification模块
1
import notificationManager from '@ohos.notificationManager'
  1. 发送通知
1
2
3
4
5
6
7
8
9
10
11
// 2.1 构建通知请求
let request: notificationManager.NotificationRequest = {
id: 10,
content: {
//通知内容
}
}
// 2.2 发布通知
notificationManager.publish(request)
.then(() => console.log('发送成功'))
.catch(reason => console.log('发送失败'JSON.stringify(reason)))
  1. 取消通知
1
2
3
4
//取消指定id的通知
notificationManager.cancel(10)
//取消当前应用的所有通知
notificationManager.cancelAll()

通知类型

通知的类型分为四种,其中后面三种为需展开的显示方式。

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

image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 普通文本型
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: '通知标题',
text: '通知内容详情',
additionalText: '通知附加内容'
}
}
// 长文本型
content: {
contentType: noticationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: {
title: '通知标题', //展开前的标题
text: '通知内容详情', //展开前的内容
additionalText: '通知附加内容',
longText: '通知中的长文本,我很长,我很长,我很长...',
briefText: '通知概要和总结', //在该模式没有作用
expandedTitle: '通知展开时的标题' //展开后的标题
}
}
//多行文本型
content: {
contentType: noticationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,
multiline: {
title: '通知标题',
text: '通知内容详情',
additionalText: '通知附加内容',
briefText: '通知概要和总结',
longTitle: '展开时的标题,有多行,很宽',
lines: [
'第一行',
'第二行',
'第三行',
'第四行',
]
}
}
//图片型
content: {
contentType: noticationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: {
title: '通知标题',
text: '通知内容详情',
additionalText: '通知附加内容',
briefText: '通知概要和总结', //在这个模式有作用,位于展开标题和图片之间
expandedTitle: '展开后的标题',
picture: this.pixel //格式为PixelMap类型
}
}
//PixelMap图片获取
async aboutToAppear() {
//获取资源管理器
let rm = getContext(this).resourceManager;
//读取图片
let file = await rm.getMediaContent($r('app.media.xxx'))
//创建PixelMap
image.createImageSource(file.buffer).createPixelMap()
.then(value => this.pixel = value)
.catch(reason => console.log('testTag', '加载图片异常', JSON.stringify(reason)))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//更多通知用法
let request: notificationManager.NotificationRequest = {
id: this.idx ++,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: '普通通知' + this.idx,
text: '普通通知内容',
additionalText: '通知附加内容'
}
},
deliveryTime: new Date().getTime(), //投递的时间
showDeliveryTime: true, //显示时间
groupName: 'WeChat', //通知组,有多个相同groupName通知会合在一起
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION //通道类型
}
类型枚举 说明 状态栏图标 提示音 横幅
SOCIAL_COMMUNICATION 社交类型
SERVICE_INFORMATION 服务类型 X
CONTENT_INFORMATION 内容类型 X X
OTHER_TYPES 其他 X X X
  • 更多使用方法可以参考鸿蒙开发文档

进度条通知

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

  1. 判断当前系统是否支持进度条模板
1
2
3
4
5
6
let isSupport = await notify.isSupportTemplate('downloadTemplate') 
//判断动作可以放在aboutToAppear()当中
if(!this.isSupport){
// 当前系统不支持进度条模板
return
}
  1. 定义通知请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 3.1 通知模板
let template = {
name: 'downloadTemplate', //模板名称,必须是downloadTemplate
data: {
progressValue: this.progressValue, //当前进度
progressMaxValue: this.progressMaxValue //进度最大值
}
}
// 3.2 通知请求
let request: notify.NotificationRequest = {
id: this.notificationId, //尽量使用固定id,不同操作需要使用通过id
template: template, // 导入模板
wantAgent: this.wantAgentInstance,
content: { //通知内容
contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: this.filename + ': ' + this.state,
text: '',
additionalText: this.progressValue + '%'
}
}
}
// 3.发送通知
notify.publish(request)
.then(() => console.log('test', '通知发送成功'))
.catch(reason => console.log('test', '通知发送失败!', JSON.stringify(reason)))
  • 在每一次任务进度变更都发送一次通知,实现进度条的变化,所有id不能变

通知意图

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 1. 意图行为信息
let wantInfo: wantAgent.WantAgentInfo = {
wants: [
{ //要拉起的是哪个,是个数组,可以有多个
device: '',
bundleName: 'com.example.myapplication', //bundleName,每个应用的标识
abilityName: 'EntryAbility', //页面abilityName
action: '',
entities: []
}
],
requestCode: 0, //请求参数码
operationType: wantAgent.OperationType.START_ABILITY, //枚举,START_ABILITY启动
wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG] //标识,CONSTANT_FLAG表示行为意图不可变
//wantAgentFlags是个数组,可以有多个
}
// 2. 创建wantAgent实例
this.wantAgentInstance = await wantAgent.getWantAgent(wantInfo)
// 3. 通知请求
let request: notify.NotificationRequest = {
id: this.notificationId,
template: template,
wantAgent: this.wantAgentInstance, //设置通知意图
content: {
//通知内容
}
}