多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

HarmonyOS 应用开发《掌上英语》第90篇:系统代理提醒(ArkTS)

HarmonyOS 应用开发《掌上英语》第90篇:系统代理提醒(ArkTS) 代理提醒(ArkTS)功能介绍应用退到后台或进程终止后仍然有一些提醒用户的定时类通知为满足此类功能场景系统提供了代理提醒的能力。当应用退至后台或进程终止后系统会代理应用做定时提醒。当前支持的提醒类型包括倒计时、日历和闹钟。约束与限制个数限制一个普通应用支持最多30个有效提醒一个系统应用支持最多10000个有效提醒。整个系统最多支持12000个有效提醒。说明当到达设置的提醒时间点时通知中心会弹出相应提醒。若未点击提醒上的关闭/CLOSE按钮则代理提醒是有效/未过期的若点击了关闭/CLOSE按钮则代理提醒过期。当代理提醒是周期性提醒时如设置每天提醒无论是否点击关闭/CLOSE按钮代理提醒都是有效的。跳转限制点击提醒通知后跳转的应用必须是申请代理提醒的本应用。与相关Kit的关系当到达设置的提醒时间点后代理提醒使用Notification Kit发布通知通知会显示在通知中心通知样式请参考Notification Kit通知样式中的文本类型。模拟器支持情况从API version 20开始本能力支持模拟器开发。接口说明以下是代理提醒的相关接口下表均以Promise形式为例更多接口及使用方式请见后台代理提醒文档。表1主要接口接口名描述publishReminder(reminderReq: ReminderRequest): Promisenumber发布后台代理提醒。cancelReminder(reminderId: number): Promisevoid取消指定id的代理提醒。getValidReminders(): PromiseArrayReminderRequest获取当前应用设置的所有有效未过期的代理提醒。cancelAllReminders(): Promisevoid取消当前应用设置的所有代理提醒。addNotificationSlot(slot: NotificationSlot): Promisevoid添加通知渠道。removeNotificationSlot(slotType: notification.SlotType): Promisevoid删除指定的通知渠道类型。开发步骤申请权限申请ohos.permission.PUBLISH_AGENT_REMINDER权限配置方式请参阅声明权限。请求通知授权请求通知授权。获得用户授权后才能使用代理提醒功能。功能开发导入模块。import { notificationManager } from kit.NotificationKit; import { reminderAgentManager } from kit.BackgroundTasksKit; import { BusinessError } from kit.BasicServicesKit;定义目标提醒代理。开发者根据实际需要选择定义如下类型的提醒。定义倒计时实例。let timer: reminderAgentManager.ReminderRequestTimer { reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER, // 提醒类型为倒计时类型 ringDuration: Constant.REMINDER_DURATION, title: context.resourceManager.getStringSync($r(app.string.timer).id), // 指明提醒标题, app.string.timer资源文件中的value值为计时器 content: context.resourceManager.getStringSync($r(app.string.countdown_close).id), // 指明提醒内容, app.string.countdown_close资源文件中的value值为计时器已结束 wantAgent: { // // 点击提醒通知后跳转的目标UIAbility信息 pkgName: com.example.reminderagentmanager, abilityName: EntryAbility }, notificationId: 100, // 指明提醒使用的通知的ID号相同ID号的提醒会覆盖 slotType: notificationManager.SlotType.CONTENT_INFORMATION, // 指明提醒的Slot类型 triggerTimeInSeconds: this.countdownTime };定义日历实例。let calendar: reminderAgentManager.ReminderRequestCalendar { reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // 提醒类型为日历类型 dateTime: { // 指明提醒的目标时间 year: date.getFullYear(), month: date.getUTCMonth() 1, day: date.getDate(), hour: date.getHours(), minute: date.getMinutes(), }, actionButton: // 设置弹出的提醒通知信息上显示的按钮类型和标题 [{ title: context.resourceManager.getStringSync($r(app.string.calendar_close).id), // app.string.calendar_close资源文件中的value值为关闭日历提醒 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE }], // 点击提醒通知后跳转的目标UIAbility信息 wantAgent: { pkgName: com.example.reminderagentmanager, abilityName: EntryAbility }, ringDuration: Constant.REMINDER_DURATION, // 指明响铃时长单位秒 title: context.resourceManager.getStringSync($r(app.string.calendar).id), // 指明提醒标题, app.string.calendar资源文件中的value值为日历 content: context.resourceManager.getStringSync($r(app.string.calendar_reach).id), // 指明提醒内容, app.string.calendar_reach资源文件中的value值为日历提醒时间到了 slotType: notificationManager.SlotType.CONTENT_INFORMATION // 指明提醒的Slot类型 }定义闹钟实例。let alarm: reminderAgentManager.ReminderRequestAlarm { reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // 提醒类型为闹钟类型 hour: time.hour, // 指明提醒的目标时刻 minute: time.minute, // 指明提醒的目标分钟 actionButton: // 设置弹出的提醒通知信息上显示的按钮类型和标题 [ { title: context.resourceManager.getStringSync($r(app.string.alarm_clock_close).id), // app.string.alarm_clock_close资源文件中的value值为关闭闹钟 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE }, { title: context.resourceManager.getStringSync($r(app.string.alarm_clock_postpone).id), // app.string.alarm_clock_postpone资源文件中的value值为推迟闹钟 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE } ], slotType: notificationManager.SlotType.CONTENT_INFORMATION, // 指明提醒的Slot类型 ringDuration: Constant.REMINDER_DURATION, // 指明响铃时长单位秒 wantAgent: { // 点击提醒通知后跳转的目标UIAbility信息 pkgName: com.example.reminderagentmanager, abilityName: EntryAbility }, title: context.resourceManager.getStringSync($r(app.string.alarm_clock).id), // 指明提醒标题, app.string.alarm_clock资源文件中的value值为闹钟 content: context.resourceManager.getStringSync($r(app.string.alarm_clock_reach).id), // 指明提醒内容, app.string.alarm_clock_reach资源文件中的value值为闹钟时间已到 snoozeTimes: 0, // 指明延迟提醒次数 timeInterval: 0, // 执行延迟提醒间隔单位秒 daysOfWeek: [] // 指明每周哪几天需要重复提醒 }发布相应的提醒代理。代理发布后应用即可使用后台代理提醒功能。let reminderId: number await reminderAgentManager.publishReminder( this.calendarReminders[index].reminderRequestCalendar!); Logger.info(TAG, publish reminder result: id is ${reminderId}); this.calendarReminders[index].reminderId reminderId; // 保存发布的提醒ID根据需要删除提醒任务。Logger.info(TAG, cancel reminder id is ${this.calendarReminders[index].reminderId}) await reminderAgentManager.cancelReminder(this.calendarReminders[index].reminderId);相关实例基于代理提醒有以下相关实例可供参考后台代理提醒ArkTSAPI9翻页时钟ArkTSAPI9
返回列表