提醒和通知:給應用添加通知和提醒
import notification from '@ohos.notificationManager';
// 通知圖片必須為PixelMap對象榕莺∠ビ可以將資源圖片轉(zhuǎn)化為PixelMap對象
let imageArray = await getContext(this).resourceManager
.getMediaContent($r('app.media.largeIcon').id);
let imageResource = image.createImageResource(imageArray.buffer);
let opts = {desiredSize:{height:72, width:72}};
let largePixelMap = await imageResource.createPixelMap(opts);
let notificationRequest : notification.NotificationRequest {
id:1000,
showDiliveryTime:true,
deliveryTime: new Date().getTime(),
content: {
contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title:'通知標題', text:"通知內(nèi)容詳情",
additionalText:'通知附加內(nèi)容',
}
},
largeIcon:largePixelMap,
smallIcon: smallPixelMap,
// 添加操作按鈕,最多可添加三個凿渊。
actionButtons: [
{title:'回復', wantAgetn: wantAgentObj}
]
}
// 發(fā)布通知
notification.publish(notificationRequest).then(() => {
console.info('publish success');
}).catch((err) => {
console.error(publish failed, code:${err.code},message:${err.message}
);
})
大圖類型的通知:
content:{
contentType: notification.ContentType.NOTIFICATION_CONTENT_PICTURE
picture: {
title: '', text:'', briefText:'', expandedTiele: '',
picture: pixelMap,
}
}
橫幅通知:在屏幕頂部懸浮5秒倍阐。
slotType: notification.SlotType.SOCIAL_COMMUNICATION,
SlotType:
SOCIAL_COMMUNICATION, 社交類型:狀態(tài)欄有圖標纵散,有橫幅和提示音竭鞍。
SERVICE_INFORMATION, 服務類型:狀態(tài)欄有圖標恋沃,沒有橫幅顺呕,有提示音枫攀。
CONTENT_INFORMATION, 內(nèi)容類型:狀態(tài)欄有圖標,沒有橫幅和提示音株茶。
OTHER_TYPES , 其他類型:狀態(tài)欄無圖標来涨,沒有橫幅和提示音。
通知分組启盛。對通知進行分組蹦掐。
groupName: 'productGroup'
進度條類型的通知技羔。
private isSupport: boolean = false;
aboutToAppear(){
// 是否支持該模板
notificationManager.isSupportTemplate('downloadTemplate').then((data)=> {
this.isSupport = data;
});
}
@State downloadStatus: number = DOWNLOAD_STATUS.INITIAL;
@State downloadProgress:number =0;
private interval:number = -1;
download() {
this.interval = setInterval(async () => {
if(this.downloadProgress === 100) {
this.notificationTitle = await getStringByRes($r('app.string.notify_title_finish'));
this.downloadStatus = DOWNLOAD_STATUS.FINISHED;
clearInterval(this.interval);
} else {
this.downloadProgress +=2;
}
if(this.isSupport) { // 發(fā)送通知。
publishNotification(this.downloadProgress, this.notificationTitle, this.wantAgentObj);
}
}, 1000);
}
import notification from '@ohos.notificationManager';
expert function publishNotification(progress:number,title:string, wantAgentObj:WantAgent) {
let template= {
name: 'downloadTemplate',
data:{progressValue:progress, progressMaxValue:100,}
};
let notificationRequest: notification.NotificationRequest = {
id:10000,
slotType:notificationManager.SlotType.CONTENT_INFORMATION,
template:template,
content: {
contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: `${title}: ${CommonConstants.DOWNLOAD_FILE}`,
text:'senTemplate',
additionalText:`${progress}%`
}
}
};
// 發(fā)布通知
notification.publish(notificationRequest).catch(err => {
Logger.error(`[ANS] publish failed, code is ${err.code}, message is ${err.message}`);
})
}
// 暫停下載
async pause() {
this.notificationTitle = await getStringByRes($r('app.string.notification_title_pause'));
clearInterval(this.interval);
this.downloadStatus = DOWNLOAD_STATUS.PAUSE;
if(this.isSupport) {
publishNotification(this.downloadProgress, this.notificationTitle, this.wantAgentObj);
}
}
// 取消下載
async clear() {
this.downloadProgress =0;
clearInterval(this.interval);
this.downloadStatus = DOWNLOAD_STATUS.INITIAL;
notitication.cancel(CommonConstants.NOTIFICATION_ID);
}
### // 給通知添加行為意圖卧抗。
import wangAgent,{WantAgent} from '@ohos.app.ability.wantAgent';
private wangAgent: WantAgent= null;
private context = getContext(this) as common.UIAbilityContext;
aboutToAppear() {
let bundleName = this.context.abilityInfo.bundleName;
let abilityName = this.context.abilityInfo.name;
createWantAgent(bundleName, abilityName).then(want =>{
this.wantAgentObj = want;
})
}
download() {
...
if(this.isSupport) { // 發(fā)送通知藤滥。
publishNotification(this.downloadProgress, this.notificationTitle, this.wantAgentObj);
}
},CommonConstants.UPDATE_FOEQUENCY);
}