1.申請ohos.permission.PUBLISH_AGENT_REMINDER權(quán)限。
2.使能通知開關(guān)。獲得用戶授權(quán)后,才能使用代理提醒功能。
3.導(dǎo)入模塊。
import reminderAgentManager from '@ohos.reminderAgentManager';
import notificationManager from '@ohos.notificationManager';
4.定義目標(biāo)提醒代理。開發(fā)者根據(jù)實際需要,選擇定義如下類型的提醒。
定義倒計時實例。
let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER, // 提醒類型為倒計時類型
triggerTimeInSeconds: 10,
actionButton: [// 設(shè)置彈出的提醒通知信息上顯示的按鈕類型和標(biāo)題
{
title: 'close',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
}
],
wantAgent: { // 點擊提醒通知后跳轉(zhuǎn)的目標(biāo)UIAbility信息
pkgName: 'com.example.myapplication',
abilityName: 'EntryAbility'
},
maxScreenWantAgent: { // 全屏顯示提醒到達時自動拉起的目標(biāo)UIAbility信息
pkgName: 'com.example.myapplication',
abilityName: 'EntryAbility'
},
title: 'this is title', // 指明提醒標(biāo)題
content: 'this is content', // 指明提醒內(nèi)容
expiredContent: 'this reminder has expired', // 指明提醒過期后需要顯示的內(nèi)容
notificationId: 100, // 指明提醒使用的通知的ID號,相同ID號的提醒會覆蓋
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot類型
}
定義日歷實例。
let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // 提醒類型為日歷類型
dateTime: { // 指明提醒的目標(biāo)時間
year: 2023,
month: 1,
day: 1,
hour: 11,
minute: 14,
second: 30
},
repeatMonths: [1], // 指明重復(fù)提醒的月份
repeatDays: [1], // 指明重復(fù)提醒的日期
actionButton: [// 設(shè)置彈出的提醒通知信息上顯示的按鈕類型和標(biāo)題
{
title: 'close',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
},
{
title: 'snooze',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
},
],
wantAgent: { // 點擊提醒通知后跳轉(zhuǎn)的目標(biāo)UIAbility信息
pkgName: 'com.example.myapplication',
abilityName: 'EntryAbility'
},
maxScreenWantAgent: { // 全屏顯示提醒到達時自動拉起的目標(biāo)UIAbility信息
pkgName: 'com.example.myapplication',
abilityName: 'EntryAbility'
},
ringDuration: 5, // 指明響鈴時長(單位:秒)
snoozeTimes: 2, // 指明延遲提醒次數(shù)
timeInterval: 5, // 執(zhí)行延遲提醒間隔(單位:秒)
title: 'this is title', // 指明提醒標(biāo)題
content: 'this is content', // 指明提醒內(nèi)容
expiredContent: 'this reminder has expired', // 指明提醒過期后需要顯示的內(nèi)容
snoozeContent: 'remind later', // 指明延遲提醒時需要顯示的內(nèi)容
notificationId: 100, // 指明提醒使用的通知的ID號,相同ID號的提醒會覆蓋
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot類型
}
定義鬧鐘實例。
let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // 提醒類型為鬧鐘類型
hour: 23, // 指明提醒的目標(biāo)時刻
minute: 9, // 指明提醒的目標(biāo)分鐘
daysOfWeek: [2], // 指明每周哪幾天需要重復(fù)提醒
actionButton: [ // 設(shè)置彈出的提醒通知信息上顯示的按鈕類型和標(biāo)題
{
title: 'close',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
},
{
title: 'snooze',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
},
],
wantAgent: { // 點擊提醒通知后跳轉(zhuǎn)的目標(biāo)UIAbility信息
pkgName: 'com.example.myapplication',
abilityName: 'EntryAbility'
},
maxScreenWantAgent: { // 全屏顯示提醒到達時自動拉起的目標(biāo)UIAbility信息
pkgName: 'com.example.myapplication',
abilityName: 'EntryAbility'
},
ringDuration: 5, // 指明響鈴時長(單位:秒)
snoozeTimes: 2, // 指明延遲提醒次數(shù)
timeInterval: 5, // 執(zhí)行延遲提醒間隔(單位:秒)
title: 'this is title', // 指明提醒標(biāo)題
content: 'this is content', // 指明提醒內(nèi)容
expiredContent: 'this reminder has expired', // 指明提醒過期后需要顯示的內(nèi)容
snoozeContent: 'remind later', // 指明延遲提醒時需要顯示的內(nèi)容
notificationId: 99, // 指明提醒使用的通知的ID號,相同ID號的提醒會覆蓋
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot類型
}
5.發(fā)布相應(yīng)的提醒代理。代理發(fā)布后,應(yīng)用即可使用后臺代理提醒功能。
reminderAgentManager.publishReminder(targetReminderAgent).then(res => {
console.info('Succeeded in publishing reminder. ');
let reminderId: number = res; // 發(fā)布的提醒ID
}).catch(err => {
console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
})
6.根據(jù)需要刪除提醒任務(wù)。文章來源:http://www.zghlxwxcb.cn/news/detail-794307.html
// reminderId的值從發(fā)布提醒代理成功之后的回調(diào)中獲得
reminderAgentManager.cancelReminder(reminderId).then(() => {
console.info('Succeeded in canceling reminder.');
}).catch(err => {
console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`);
});
本文根據(jù)HarmonyOS官方開發(fā)文檔學(xué)習(xí)整理文章來源地址http://www.zghlxwxcb.cn/news/detail-794307.html
到了這里,關(guān)于鴻蒙原生應(yīng)用/元服務(wù)開發(fā)-代理提醒開發(fā)步驟(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!