提醒和通知:給應用添加通知和提醒

提醒和通知:給應用添加通知和提醒

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);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市社裆,隨后出現(xiàn)的幾起案子拙绊,更是在濱河造成了極大的恐慌,老刑警劉巖泳秀,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件标沪,死亡現(xiàn)場離奇詭異,居然都是意外死亡嗜傅,警方通過查閱死者的電腦和手機金句,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吕嘀,“玉大人违寞,你說我怎么就攤上這事∨挤浚” “怎么了坞靶?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蝴悉。 經(jīng)常有香客問我,道長瘾敢,這世上最難降的妖魔是什么拍冠? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮簇抵,結(jié)果婚禮上庆杜,老公的妹妹穿的比我還像新娘。我一直安慰自己碟摆,他們只是感情好晃财,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著典蜕,像睡著了一般断盛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上愉舔,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天钢猛,我揣著相機與錄音,去河邊找鬼轩缤。 笑死命迈,一個胖子當著我的面吹牛贩绕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼门岔!你這毒婦竟也來了慷蠕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤劫狠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迂尝,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年剪芥,在試婚紗的時候發(fā)現(xiàn)自己被綠了垄开。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡税肪,死狀恐怖溉躲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情益兄,我是刑警寧澤锻梳,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站净捅,受9級特大地震影響疑枯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛔六,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一荆永、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧国章,春花似錦具钥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至四啰,卻和暖如春宁玫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背柑晒。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工撬统, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人敦迄。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓恋追,卻偏偏與公主長得像凭迹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子苦囱,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容