- 默认一安装是没有通知权限的
- 只会弹一次,下次不会再弹了
- 可以根据groupname 来把不同的通知规定到一个里面
是否开启了通知
- notificationManager.isNotificationEnabled 使用callback异步回调,也可以使用使用Promise异步回调
- notificationManager.isNotificationEnabledSync 同步获取通知使能状态。
api12beta 还没有如此api,文档上有
notificationManager.isNotificationEnabled().then((data) => {
this.isNotificationEnabled = data
}).catch((error: Error) => {
})
请求获取通知权限
只会弹一次弹窗,跟权限一样
- requestEnableNotification ,使用callback异步回调 或者 Promise异步回调
notificationManager.requestEnableNotification().then((data) => {
this.isNotificationRequestEnable = '成功,有'
}).catch((error: BusinessError) => {
this.isNotificationRequestEnable = '失败,没有'
});
去设置页面打开
如果用户拒绝了,就有可能需要用户自己手动打开,跟权限一样,pushParams是你自己的bundleName
AlertDialog.show({
title: "权限申请",
message: '去设置页面打开',
primaryButton: {
value: '取消', action: () => {
promptAction.showToast({ message: '取消了' })
}
},
secondaryButton: {
value: '确定', action: () => {
let wantInfo: Want = {
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'application_info_entry',
parameters: {
pushParams: 'com.nzy.test' // 打开指定应用的详情页面 这是你自己的bundleName
}
}
let context = getContext(this) as common.UIAbilityContext
context.startAbility(wantInfo)
}
}
})
发布通知
notificationManager.publish
- ContentType.NOTIFICATION_CONTENT_BASIC_TEXT 普通类型通知。
- ContentType.NOTIFICATION_CONTENT_LONG_TEXT 长文本类型通知。
- ContentType.NOTIFICATION_CONTENT_PICTURE 图片类型通知。(预留能力,暂未支持)。
- ContentType.NOTIFICATION_CONTENT_CONVERSATION 社交类型通知。(预留能力,暂未支持
- ContentType.NOTIFICATION_CONTENT_MULTILINE 多行文本类型通知
- ContentType.NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW 实况窗类型通知。(预留能力,暂未支持)。
- ContentType.NOTIFICATION_CONTENT_LIVE_VIEW 普通实况窗类型通知。(预留能力,暂未支持)
下面是发布普通文本,长文本,多行文本的demo
Button('发布文本通知').onClick(() => {
let publishCallback = (err: BusinessError): void => {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("publish success");
}
}
//通知Request对象
let notificationRequest: notificationManager.NotificationRequest = {
id: 1,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: "test_title",
text: "test_text",
additionalText: "test_additionalText"
}
}
};
notificationManager.publish(notificationRequest, publishCallback);
})
Button('发布长文本通知').onClick(() => {
let publishCallback = (err: BusinessError): void => {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("publish success");
}
}
//通知Request对象
let notificationRequest: notificationManager.NotificationRequest = {
id: 2,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: {
text: 'text',
title: 'title',
briefText: "briefText",
longText: "longText",
expandedTitle: "expandedTitle"
}
}
};
notificationManager.publish(notificationRequest, publishCallback);
})
Button('发送多行文本').onClick(() => {
let publishCallback = (err: BusinessError): void => {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("publish success");
}
}
//通知Request对象
let notificationRequest: notificationManager.NotificationRequest = {
id: 3,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,
multiLine: {
text: 'text',
title: 'title',
briefText: "briefText",
lines: ['文案1', '文案1', '文案1', '文案1', '文案1', '文案1', '文案1', '文案1', '文案1'],
longTitle: 'longTitle'
}
}
};
notificationManager.publish(notificationRequest, publishCallback);
})
取消
- notificationManager.cancel 根据id取消
- notificationManager.cancelAll 取消所有的
- notificationManager.cancelGroup('groupName')
设置角标
notificationManager.setBadgeNumber(100);
获取所有的通知个数
notificationManager.getActiveNotificationCount().then((data)=>{
promptAction.showToast({message:data.toString()})
})