三、代理提醒
传送门
1.什么是代理提醒
- 应用退到后台或进程终止后,仍然有一些提醒用户的定时类任务,例如购物类应用抢购提醒等,为满足此类功能场景,系统提供了代理提醒(reminderAgentManager)的能力。当应用退至后台或进程终止后,系统会代理应用做相应的提醒。当前支持的提醒类型包括:
- 倒计时:基于倒计时的提醒功能
- 日历:基于日历的提醒功能
- 闹钟:基于时钟的提醒功能
- 不同的代理提醒主要是通过 reminderType 来设置
- 倒计时 reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER
- 日历 reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR
- 闹钟 reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM
- 倒计时类, 日历类, 闹钟类 代理提醒的操作步骤是一样的, 只不过最初始定义的对象内容不一样。
2.代理提醒限制
- 一个三方应用支持最多30个有效提醒(有效即发布成功),一个系统应用支持最多10000个有效提醒,整个系统最多支持12000个有效提醒
- 点击提醒通知后跳转的应用必须是申请代理提醒的本应用
- 需要一个通知权限, 因为不管是闹钟还是倒计时, 到位以后需要一个提示出现,当你没有申请通知的时候, 这个代理提醒是不会生效的
3.代理提醒api
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/453c3f42a625495dad16e0af4483850e.png#pic_center)
4.开启代理提醒
4.1 引入依赖
import { reminderAgentManager } from '@kit.BackgroundTasksKit'
import { notificationManager } from '@kit.NotificationKit'
4.2 通知权限申请
notificationManager
.isNotificationEnabled()
.then((data: boolean) => {
if (!data) {
notificationManager
.requestEnableNotification(this.context)
.then(() => {
})
.catch(() => console.log('wuwuwu => 用户选择禁用按钮了'))
} else {
this.sendReminder()
}
})
.catch(() => {
console.log('wuwuwu => 检测到目前通知权限为禁用状态')
})
4.3 创建 agent 对象
@State targetReminderAgent: reminderAgentManager.ReminderRequestTimer = Object()
@State targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = Object()
@State targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = Object()
4.3 倒计时类提醒
this.targetReminderAgent = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
triggerTimeInSeconds: 15,
title: 'title',
content: 'content',
notificationId: 111111,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
actionButton: [
{
title: '关闭按钮',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
}
],
wantAgent: {
pkgName: 'com.example.mytask',
abilityName: 'EntryAbility'
}
}
4.4 日历类提醒
this.targetReminderAgent = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR,
dateTime: {
year: 2024,
month: 8,
day: 12,
hour: 15,
minute: 30,
second: 10
},
repeatMonths: [8],
repeatDays: [12],
wantAgent: {
pkgName: 'com.example.mybackgroundtask',
abilityName: 'EntryAbility'
},
ringDuration: 5,
snoozeTimes: 2,
timeInterval: 5,
title: 'this is title',
content: 'this is content',
expiredContent: 'this reminder has expired',
snoozeContent: 'remind later',
notificationId: 1100,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
}
4.5 闹钟类提醒
this.targetReminderAgent = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM,
hour: 15,
minute: 50,
daysOfWeek: [1],
wantAgent: {
pkgName: 'com.example.mybackgroundtask',
abilityName: 'EntryAbility'
},
actionButton: [
{
title: 'close',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
},
{
title: 'snooze',
type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
},
],
ringDuration: 5,
snoozeTimes: 2,
timeInterval: 5,
title: 'this is title',
content: 'this is content',
expiredContent: 'this reminder has expired',
snoozeContent: 'remind later',
notificationId: 99,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
}
4.6 发送提醒
reminderAgentManager.publishReminder(this.targetReminderAgent)
.then(() => {
console.log('wuwuwu => 发布定时提醒成功 ^_^')
})
.catch((err: BusinessError) => {
console.log('wuwuwu => 发布定时提醒失败 : ' + JSON.stringify(err))
})