用户通知服务
简介
- 将应用产生的通知直接在客户端本地推送给用户,本地通知根据通知类型及发布场景会产生对应的铃声、震动、横幅、锁屏、息屏、通知栏提醒和显示。
能力
- 文本通知
- 纯文本通知( 短文本类型(单行文本), 长文本类型(多行文本) )
- 图文通知
- 进度条通知
- 特殊的通知( 角标 )
- 过程
- 当前用用需要获取用户授权, 才能发送通知
- 授权成功才可以正常发送通知
- 给了我们几个 API
=> 查看是否有授权的 API, isNotificationEnabled()
=> 发送通知的方法 requestEnableNotification()
-> 向用户请求发送通知, 第一次请求的时候会弹窗
做消息通知之前首先要了解一下通知类型和通道类型(通知方式:app小圆点、状态栏图标、横幅、提示音)
1.通知类型
- 实际上就是设置通知卡片的类型
- 设置手机下拉任务栏界面,通知卡片的样式和内容会有所不同
ContentType | 值 | 说明 |
---|
NOTIFICATION_CONTENT_BASIC_TEXT | 0 | 普通类型通知 |
NOTIFICATION_CONTENT_LONG_TEXT | 1 | 长文本类型通知 |
NOTIFICATION_CONTENT_PICTURE | 2 | 图片类型通知 |
NOTIFICATION_CONTENT_CONVERSATION | 3 | 社交类型通知。预留能力,暂不支持 |
NOTIFICATION_CONTENT_MULTILINE | 4 | 多行文本 |
NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW | 5 | 实况窗类型通知。不支持三方应用直接创建该类型通知,可以由系统代理创建系统实况窗类型通知后,三方应用发布同ID的通知来更新指定内容。 |
NOTIFICATION_CONTENT_LIVE_VIEW | 6 | 普通实况窗类型通知。只支持系统应用 |
2.通道类型(通知方式:app小圆点、状态栏图标、横幅、提示音)
SlotType | SlotTypeVal | SlotType说明 | SlotLevel |
---|
UNKNOWN_TYPE | 0 | 未知类型 | LEVEL_MIN |
SOCIAL_COMMUNICATION* | 1 | 社交通信 | LEVEL_HIGH |
SERVICE_INFORMATION* | 2 | 服务提醒 | LEVEL_DEFAULT |
CONTENT_INFORMATION* | 3 | 内容资讯 | LEVEL_MIN |
LIVE_VIEW | 4 | 实况窗,不支持三方应用直接创建该渠道类型通知,可以由系统代理创建后,三方应用发布同ID的通知来更新指定内容。 | LEVEL_DEFAULT |
CUSTOMER_SERVICE | 5 | 客服消息。该类型用于用户与商家之间的客服消息,需由用户主动发起。 | LEVEL_LOW |
OTHER_TYPES* | 0xFFFF | 其他 | LEVEL_MIN |
slotLevel:通知优先级,优先级越高
SlotLevel | SlotLevelVal | SlotLevel说明 |
---|
LEVEL_NONE | 0 | 表示关闭通知功能 |
LEVEL_MIN | 1 | 1.状态栏中不显示通知图标 2.没有横幅 3.没有提示音 |
LEVEL_LOW | 2 | 1.状态栏中显示通知图标 2.没有横幅 3.没有提示音 |
LEVEL_DEFAULT | 3 | 1.状态栏中显示通知图标 2.没有横幅 3.有提示音 |
LEVEL_HIGH | 4 | 1.状态栏中显示通知图标 2.有横幅 3.有提示音 |
3.请求通知授权
- 1.通过固定api(notificationManager.isNotificationEnabled)查询是否已授权
- 2.若未授权,通过固定api(notificationManager.requestEnableNotification)请求用户授权
- 3.授权成功后,才能发送通知
import { abilityAccessCtrl, common } from '@kit.AbilityKit';
isEnabled(callback:()=>void){
notificationManager.isNotificationEnabled()
.then((res)=>{
console.log('用户授权 => ',JSON.stringify(res));
if(!res){
notificationManager.requestEnableNotification(this.context)
.then(()=>{
console.log('发送通知1 => ');
callback()
})
.catch(()=>{
console.log('不允许发送通知 => ');
const temp = abilityAccessCtrl.createAtManager()
temp.requestPermissionsFromUser(this.context, ['ohos.permission.NOTIFICATION_CONTROLLER'])
})
} else {
console.log('发送通知2 => ');
callback()
}
})
.catch(()=>{
console.log('=> 查询失败')
})
}
4.发送通知–在获取权限的情况下
import { notificationManager } from '@kit.NotificationKit';
const notificationInfo: notificationManager.NotificationRequest = {
id: ++this.count,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
text: '通知的内容' + this.count,
title: '通知的标题' + this.count,
}
},
notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
groupName: 'Aaa'
}
notificationManager.publish(notificationInfo)
5.进度条通知
sendSchedule(){
this.downloadCount += 1
const template:notificationManager.NotificationTemplate = {
name: 'downloadTemplate',
data:{
title: '下载内容',
fileName: '下载完成',
progressValue: this.downloadCount * 10,
progressMaxValue: 100,
}
}
const notificationInfo:notificationManager.NotificationRequest = {
id:this.count,
content:{
notificationContentType:notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
text: '正在下载文件',
title: '下载文件',
}
},
template,
}
notificationManager.publish(notificationInfo, (err) => {
console.log('wuwuwu => 通知发送成功')
})
}
6.点击通知调起ability
- 首先需要创建want对象
- 然后就还是正常发通知的步骤
pullUpAbility() {
const wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
bundleName: 'com.example.testnitification',
abilityName: 'EntryAbility',
parameters: {
txt: 'aaasssaa'
}
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
}
wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
if (err) {
console.log('wuwuwu 创建 WantAgent 出错 =>');
return
}
const notificationInfo: notificationManager.NotificationRequest = {
id: ++this.count,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: {
title: '长文本title',
text: '长文本text',
briefText: '长文本briefText',
longText: 'longText',
expandedTitle: 'expandedTitle'
}
},
notificationSlotType: notificationManager.SlotType.CUSTOMER_SERVICE,
wantAgent: data
}
notificationManager.publish(notificationInfo, () => {
console.log('wuwuwu 发送通知 => ');
})
})
}
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
if (want?.parameters) {
console.log('wuwuwu onCreate => ', JSON.stringify(want.parameters));
}
}
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
if (want?.parameters) {
console.log('wuwuwu onNewWant => ', JSON.stringify(want.parameters));
}
}