使用用户通知服务notificationManager,管理通知渠道配置为SOCIAL\_COMMUNICATION类型,应用触发通知时状态栏没有提示,且没有提示音(声音是打开的)
public startModemStatusListener() {
LogUtils.i('[LogCapture.startModemStatusListener]', 'startModemStatusListener!');
//设置通知权限
let context = GlobalContext.getContext().getObject('context') as common.UIAbilityContext;
notificationManager.isNotificationEnabled().then((data: boolean) => {
console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
if(!data){
notificationManager.requestEnableNotification(context).then(() => {
hilog.info(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification success`);
}).catch((err : Base.BusinessError) => {
if(this.notificationErrorCode == err.code){
console.info(`[ANS] requestEnableNotification refused`);
} else {
console.error(`[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
}
});
}
}).catch((err : Base.BusinessError) => {
console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`);
});
// 通知渠道 addslot回调
let addSlotCallBack = (err: Base.BusinessError): void => {
if (err) {
console.error(`addSlot failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("addSlot success");
}
}
notificationManager.addSlot(notificationManager.SlotType.CUSTOMER_SERVICE, addSlotCallBack);
//五分钟检查一次modem是否是开启状态
this.modemListenerId = setInterval(async () => {
if (true) {
//设置角标
let badgeNumber = 1;
notificationManager.setBadgeNumber(badgeNumber, this.setBadgeNumberCallback);
let notificationRequest: notificationManager.NotificationRequest = {
id: this.notificationId,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
normal: {
title: 'FiledTest Notification',
text: 'modem is close,please deal',
additionalText: 'test_additionalText',
}
}
};
notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
if (err) {
console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Succeeded in publishing notification.');
});
}
}, 10000)
}
1.SlotType配置为SOCIAL\_COMMUNICATION后会支持横幅
2.App默认没有横幅,打开横幅需手动设置,在应用对应的通知管理中的提醒方式中进行设置
3.在默认没有横幅的前提下,希望尽可能的让用户感知到消息,当前不支持,可以提需求。