场景介绍
开发者在应用开发时,可以调用后台代理提醒类ReminderRequest去创建定时提醒,包括倒计时、日历、闹钟三种提醒类型。使用后台代理提醒能力后,应用可以被冻结或退出,计时和弹出提醒的功能将被后台系统服务代理。
接口说明
ReminderRequest涉及的基础类包括ReminderHelper、ReminderRequestTimer、ReminderRequestCalendar、ReminderRequestAlarm,基础类之间的关系如下图所示:
图1 Reminder基础类关系图
![](https://myxmkj.com/wp-content/uploads/2021/12/0000000000011111111.20211022162335.03973191179411185343073995940418.png)
ReminderHelper:封装了发布、取消提醒类通知的方法。
![](https://myxmkj.com/wp-content/uploads/2021/12/b1-1.png)
ReminderRequest:后台代理提醒类基类,封装了提醒相关的属性查询和设置的操作。
![](https://myxmkj.com/wp-content/uploads/2021/12/b2-1-674x1024.png)
ReminderRequestTimer:提醒类子类,用于倒计时提醒。
![](https://myxmkj.com/wp-content/uploads/2021/12/b3-1-1024x658.png)
开发步骤
- 声明使用权限
使用后台代理提醒需要在配置文件中声明需要此权限:
"reqPermissions": [ {"name": "ohos.permission.PUBLISH_AGENT_REMINDER" }]
创建一个提醒的步骤如下:
// 1. 设置渠道信息
NotificationSlot slot = new NotificationSlot("slot_id", "slot_name", NotificationSlot.LEVEL_HIGH);
slot.setEnableLight(false);
slot.setEnableVibration(true);
// 2. 向代理服务添加渠道对象
try {
ReminderHelper.addNotificationSlot(slot);
} catch (RemoteException e) {
e.printStackTrace();
}
// 3. 创建提醒类通知对象
int[] repeatDay = {};
ReminderRequest reminder = new ReminderRequestAlarm(10, 30, repeatDay);
// 4. 设置提醒内容
reminder.setTitle("set title here").setContent("set content here");
// 5. 设置提醒时长等属性
reminder.setSnoozeTimes(1).setTimeInterval(5 * 60).setRingDuration(10);
// 6. 设置IntentAgent, (假设包名为:com.ohos.aaa,Ability类名为FirstAbility)
reminder.setIntentAgent("com.ohos.aaa", FirstAbility.class.getName());
// 7. 设置提醒信息框中的“延迟提醒”和“关闭”按钮(可选)(ActionButton)
reminder.setActionButton("snooze", ReminderRequest.ACTION_BUTTON_TYPE_SNOOZE).setActionButton("close", ReminderRequest.ACTION_BUTTON_TYPE_CLOSE);
// 8. 发布提醒类通知
try {
ReminderHelper.publishReminder(reminder);
} catch (ReminderManager.AppLimitExceedsException e) {
e.printStackTrace();
} catch (ReminderManager.SysLimitExceedsException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
创建一个倒计时提醒示例如下:
// 经过1分钟后提醒
ReminderRequest reminderRequestTimer = new ReminderRequestTimer(60);
创建一个一次性日历提醒的示例如下:
// 2021年3月2日14点30分提醒
int[] repeatMonths = {};
int[] repeatDays = {};
ReminderRequestCalendar reminderRequestCalendar = new ReminderRequestCalendar( LocalDateTime.of(2021, 3, 2, 14, 30), repeatMonths, repeatDays);
创建一个重复的日历提醒的示例如下:
// 3月份,5月份的9号和15号 14点30分提醒,延迟10分钟后再次提醒,默认延迟次数为3次
int[] repeatMonths = {3, 5};
int[] repeatDaysOfMonth = {9, 15};
ReminderRequestCalendar reminderRequestCalender = new ReminderRequestCalendar( LocalDateTime.of(2021, 3, 2, 14, 30), repeatMonths, repeatDaysOfMonth);
reminderRequestCalender.setTimeInterval(10 * 60);
创建一个一次性闹钟提醒的示例如下:
// 13点59分提醒,如果当前时间大于13点59分,则取后一天的13点59分
int[] repeatDay = {};
ReminderRequest reminderRequestAlarm = new ReminderRequestAlarm(13, 59, repeatDay);
创建一个重复的闹钟提醒的示例如下:
// 每周1,2,3,4的13点59分提醒
int[] repeatDay = {1, 2, 3, 4};
ReminderRequest reminderRequestAlarm = new ReminderRequestAlarm(13, 59, repeatDay);
创建一个用于延迟提醒的ActionButton界面的示例如下:
reminderRequest.setActionButton("snooze", ReminderRequest.ACTION_BUTTON_TYPE_SNOOZE);
创建一个用于关闭提醒的ActionButton界面的示例如下:
reminderRequest.setActionButton("close", ReminderRequest.ACTION_BUTTON_TYPE_CLOSE);
注意事项
- notificationId相同的不同NotificationRequest请求,在通知栏展示的内容会被覆盖,对于提醒来说,可能不希望被覆盖,开发时可以注意设置不同的notificationId
- 倒计时不支持持久化,系统重启后,所有倒计时失效