首页 > 其他分享 >后台提醒与代理提醒:HarmonyOS Next 的智能提醒管理

后台提醒与代理提醒:HarmonyOS Next 的智能提醒管理

时间:2024-10-27 11:00:09浏览次数:1  
标签:提醒 notificationManager reminderAgentManager err title 代理 Next HarmonyOS

本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。

在移动应用开发中,及时有效的提醒功能对于提升用户体验至关重要。HarmonyOS Next 提供了代理提醒(Agent-powered Reminder)功能,它允许应用在后台被挂起或进程终止后,由系统代理应用执行提醒任务,例如倒计时、日历、闹钟等。这种机制可以确保用户及时收到重要的提醒信息,同时避免应用过度消耗设备资源。

代理提醒的类型与开发步骤

HarmonyOS Next 支持三种类型的代理提醒:

  • 倒计时提醒:基于倒计时的提醒功能,例如倒计时 10 秒后提醒用户。
  • 日历提醒:基于日历事件的提醒功能,例如在特定日期和时间提醒用户。
  • 闹钟提醒:基于时钟的提醒功能,例如每天早上 7 点提醒用户起床。
    开发步骤
  1. 申请权限:在应用配置文件中添加 ohos.permission.PUBLISH_AGENT_REMINDER 权限。
  2. 请求通知授权:获得用户授权后,才能使用代理提醒功能。
  3. 定义提醒内容:根据需要定义倒计时、日历或闹钟提醒内容,包括提醒标题、内容、过期内容、通知渠道等。
  4. 发布提醒:调用 publishReminder 接口发布提醒任务。
  5. 取消提醒:根据需要调用 cancelRemindercancelAllReminders 接口取消提醒任务。

示例代码:倒计时提醒、日历提醒、闹钟提醒的设置

以下代码示例展示了如何设置三种类型的代理提醒:
倒计时提醒

import { reminderAgentManager } from '@kit.BackgroundTasksKit';
import { notificationManager } from '@kit.NotificationKit';
let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = {
  reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
  triggerTimeInSeconds: 10,
  actionButton: [{ title: 'close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE }],
  wantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
  maxScreenWantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
  title: 'this is title',
  content: 'this is content',
  expiredContent: 'this reminder has expired',
  notificationId: 100,
  slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
  console.info('Succeeded in publishing reminder. ');
  let reminderId: number = res; // 发布的提醒 ID
}).catch((err: BusinessError) => {
  console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
});

日历提醒

import { reminderAgentManager } from '@kit.BackgroundTasksKit';
import { notificationManager } from '@kit.NotificationKit';
let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = {
  reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR,
  dateTime: {
    year: 2023,
    month: 1,
    day: 1,
    hour: 11,
    minute: 14,
    second: 30
  },
  repeatMonths: [1],
  repeatDays: [1],
  actionButton: [
    { title: 'close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE },
    { title: 'snooze', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE }
  ],
  wantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
  maxScreenWantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
  ringDuration: 5,
  snoozeTimes: 2,
  timeInterval: 5 * 60,
  title: 'this is title',
  content: 'this is content',
  expiredContent: 'this reminder has expired',
  snoozeContent: 'remind later',
  notificationId: 100,
  slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
  console.info('Succeeded in publishing reminder. ');
  let reminderId: number = res; // 发布的提醒 ID
}).catch((err: BusinessError) => {
  console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
});

闹钟提醒

import { reminderAgentManager } from '@kit.BackgroundTasksKit';
import { notificationManager } from '@kit.NotificationKit';
let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = {
  reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM,
  hour: 23,
  minute: 9,
  daysOfWeek: [2],
  actionButton: [
    { title: 'close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE },
    { title: 'snooze', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE }
  ],
  wantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
  maxScreenWantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
  ringDuration: 5,
  snoozeTimes: 2,
  timeInterval: 5 * 60,
  title: 'this is title',
  content: 'this is content',
  expiredContent: 'this reminder has expired',
  snoozeContent: 'remind later',
  notificationId: 99,
  slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
  console.info('Succeeded in publishing reminder. ');
  let reminderId: number = res; // 发布的提醒 ID
}).catch((err: BusinessError) => {
  console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
});

表格:代理提醒类型对比

提醒类型 触发方式 重复设置 通知按钮 适用场景
倒计时提醒 倒计时结束 不支持 关闭 临时提醒,例如会议倒计时
日历提醒 指定日期和时间 支持按月或按日重复 关闭、延时 定期提醒,例如生日、纪念日
闹钟提醒 指定时间 支持按周重复 关闭、延时 每日提醒,例如起床闹钟

提醒的通知管理与优化

开发者可以使用 NotificationSlot 来管理提醒通知的样式和渠道。通过设置不同的 NotificationSlot,开发者可以创建个性化、多样化的通知样式,并选择合适的渠道进行通知,例如系统通知栏、桌面小组件等。
代码示例

import { notificationManager } from '@kit.NotificationKit';
let slot: notificationManager.Slot = {
  slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
  slotId: 1,
  name: 'My Notification Slot',
  importance: notificationManager.Importance.HIGH,
  description: 'This is my custom notification slot'
};
notificationManager.addNotificationSlot(slot).then(() => {
  console.info('Notification slot added successfully');
}).catch((err: BusinessError) => {
  console.error(`Failed to add notification slot. Code: ${err.code}, message: ${err.message}`);
});

代理提醒权限的申请方法

为了防止代理提醒功能被滥用,HarmonyOS Next 对其进行了限制和规范。开发者需要向华为官方申请代理提醒权限,才能使用该功能。
申请方法

  1. 通过 [email protected] 邮箱向华为官方申请。
  2. 邮件主题:【代理提醒权限申请】
  3. 邮件正文:包含企业名称、应用名称、应用包名、使用场景、通知标题、通知文本、通知场景、通知频率等信息。

总结

代理提醒为 HarmonyOS Next 提供了一种智能的提醒管理方式,它可以有效地提升用户体验,并避免应用过度消耗设备资源。咱们可以根据实际需求选择合适的代理提醒类型,并结合 NotificationSlot 进行通知管理和优化。同时,咱们也需要注意代理提醒的权限申请和使用规范,避免滥用该功能。

标签:提醒,notificationManager,reminderAgentManager,err,title,代理,Next,HarmonyOS
From: https://www.cnblogs.com/samex/p/18508051

相关文章

  • HarmonyOS:三方库的基本使用(1)
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18504527➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS:Node-API典型场景开发(2)
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18504462➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS:Node-API典型场景开发(1)
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18504101➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS:Node-API实现跨语言交互(3)使用Node-API实现跨语言交互开发流程
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18504008➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS:Node-API实现跨语言交互(2)Node-API支持的数据类型和接口
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18502733➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS:Node-API实现跨语言交互(1)Node-API简介
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18503923➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS NEXT实战教程:菜谱App
    随着鸿蒙系统5.0的发布,兼容的机型越来越多,对于开发者来说机会也越来越多,大家不要气馁,学习鸿蒙肯定会有用武之地,我们要做的就是做好准备。今天跟大家分享实战教程是一个菜谱App。首页这个页面可能会让初学者望而生畏,看起来比较复杂。但是仔细分析一下并不太难。幽蓝君再啰嗦一句......
  • HarmonyOS:Node-API实现跨语言交互
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18502733➤如果链接不是为敢技术的博客园地址,则可能是......
  • HarmonyOS:给您的应用添加通知(2)
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/18502620➤如果链接不是为敢技术的博客园地址,则可......
  • 构建更加丰富的页面 习题答案<HarmonyOS第一课>
    一、判断题1. Tabs组件可以通过接口传入一个TabsController,该TabsController可以控制Tabs组件进行页签切换。正确(True)错误(False)正确(True)回答正确2. WebviewController提供了变更Web组件显示内容的接口,例如可以使用loadData来加载一个网页链接地址改变Web组件的......