最新鸿蒙开发Dev5.0——通知
进度条类型通知则用于显示任务进度或下载进度等信息。除了包含基础类型通知的内容外,它还可以添加一个进度条,以便用户了解任务的进度情况。这种通知适用于文件下载、长任务处理等场景,可以动态显示进度变化,帮助用户更直观地跟踪任务进度。基础类型通知是一种简单的通知样式,用于显示重要的文本信息或简短的通知内容。它通常包含标题、内容和图标,用户可以通过点击通知来执行相关操作。我们可以给通知或其中的按钮设置的行为
·
基础通知
-
基础类型通知是一种简单的通知样式,用于显示重要的文本信息或简短的通知内容。它通常包含标题、内容和图标,用户可以通过点击通知来执行相关操作。这种通知适合用于提醒用户待办事项、设备状态、新消息等。
-
类型枚举 说明 NOTIFICATION_CONTENT_BASIC_TEXT 普通文本型 NOTIFICATION_CONTENT_LONG_TEXT 长文本文型 NOTIFICATION_CONTENT_MULTILINE 多行文本型 NOTIFICATION_CONTENT_PICTURE 图片型
-
使用基础通知的一般步骤:
-
导入notificationManager
-
import notificationManager from '@ohos.notificationManager' -
发布通知
-
// 构建通知结束 let request: notificationManager.NotificationRequest = { id: 10, content:{ //通知内容 } } // 发布通知 notificationManger.publish(request) .then( () => console.log('发送通知成功')) .catch( reason => console.log('发送通知失败', JSON.stringify(reason)))
-
取消通知
-
//取消指定id的通知 notificationMaager.cancel(10) //取消当前应用的所有通知 notificationManager.cancelAll()
-
1. 普通文本型
let request: notificationManager.NotificationRequest = {
id: 10,
content:{
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal:{
title:'通知标题',
text: '通知内容详情',
additionText: '通知附加内容'
}
},
showDeliveryTime: true,
deliveryTime: new Date().getTime(),
groupName: 'wechat',
notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION
}

groupName——分组功能 ,分为一个组的通知会集中显示

2. 长文本型
let request: notificationManager.NotificationRequest = {
id: 10,
content:{
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText:{
title:'通知标题',
text: '通知内容详情',
additionText: '通知附加内容',
longTitle: '通知中的长文本,我很长,很长,很长。。。。。。。。。。。。。。。。。。。。。。。。。。',
briefText: '通知概要和总结',
expandedTitle: '通知展开时的标题'
}
}
}

3. 多行文本
let request: notify.NotificationRequest = {
id: this.idx++,
content:{
notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_MULTILINE,
multiLine:{
title: '通知标题' + this.idx,
text: '通知内容详情',
additionalText: '通知附加内容',
longTitle: '展开后的标题',
briefText: '通知概要和总结',
lines:[
'第一行',
'第二行',
'第三行',
'第四行',
]
}
},
showDeliveryTime: true,
deliveryTime: new Date().getTime(),
notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION
}
this.publish(request)

4. 图片型
- 将本地图标资源 转化为 PixelMap对象
- 注意:不能直接使用 .png 等格式的图片资源
let request: notify.NotificationRequest = {
id: this.idx++,
content: {
notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: {
title: '通知标题' + this.idx,
text: '通知内容详情',
additionalText: '通知附加内容',
briefText: '通知概要和总结',
expandedTitle: '展开后标题' + this.idx,
picture: this.pixel
}
},
showDeliveryTime: true,
deliveryTime: new Date().getTime(),
notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION
}
this.publish(request)
| 类型枚举 | 说明 | 状态栏图标 | 提示音 | 横幅 |
|---|---|---|---|---|
| SOCIAL_COMMUNICATION | 社交类型 | √ | √ | √ |
| SERVICE_INFORMATION | 服务类型 | √ | √ | × |
| CONTENT_INFORMATION | 内容类型 | √ | × | × |
| OTHER_TYPES | 其他 | × | × | × |
进度条通知
-
进度条通知会展示一个动态的进度条,主要用于文件下载,长任务处理的进度显示
-
判断当前系统是否支持进度条模板
this.isSupport = await notificationManger.isSupportTemplate('downloadTemplate') if( !this.isSuport){ return }NotificationTemplate属性
名称 类型 只读 可选 说明 name string 否 否 模板名称。当前仅支持'downloadTemplate':下载模板。 data Record<string, Object> 否 否 模板数据。
- title: 表示下载标题。必填字段,值为字符串类型。
- fileName: 表示下载文件名。必填字段,值为字符串类型。
- progressValue: 表示下载进度,值为数值类型。
-
定义通知请求
//3.1.准备进度条模板的参数 let request: notify.NotificationRequest = { id: this.notificationId, template:{ name: 'downloadTemplate', data: { title: '123', fileName: "123", progressValue: this.progressValue, } }, wantAgent: this.wantAgentInstance, content: { notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: this.filename + ': ' + this.state, text: '', additionalText: this.progressValue + '%' } } } // 3.2通知请求 let request: notificationManager.NotificationRequest = { id: 999 template: template, content: { notification.ContentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: this.filename + ': ' this.state, text: '123', additionalText: `${this.progressValue}%`, } } } -
通知意图
-
我们可以给通知或其中的按钮设置的行为意图(Want),从而实现拉起应用组件或发布公共事件等能力
// 1.意图行为信息 let wantInfo = { wants:[ { devicedId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', action: '', entities: [] } ], operationType: wantAgent.OperationType.START_ABILITY //操作类型,启动应用,发布公共事件等 requestCode: 0, wantAentFlags: [wantAgent.WantAgentFlags.CONSTRANT_FLAG] //标识行为意图的信息的 } // 2.创建wantAgent实例 this.wantAgentInstance = await wantAgent.getWantAgent(wantInfo) // 3.通知请求 let request: notify.NotificationRequest = { id: 999, template: template, wantAgent: this.wantAgentInstance, content:{ //... } }
更多推荐

所有评论(0)