鸿蒙开发接口公共事件与通知:【@ohos.reminderAgent (后台代理提醒)】

 后台代理提醒

本模块提供后台代理提醒的能力。

开发应用时,开发者可以调用后台提醒发布的接口创建定时提醒,包括倒计时、日历、闹钟三种提醒类型。使用后台代理提醒能力后,应用可以被冻结或退出,计时和弹出提醒的功能将被后台系统服务代理。

icon-note.gif

  说明:  本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import reminderAgent from'@ohos.reminderAgent';

开发前请熟悉鸿蒙开发指导文档gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

reminderAgent.publishReminder

publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void

发布一个后台代理提醒,使用callback方式实现异步调用。

需要权限: ohos.permission.PUBLISH_AGENT_REMINDER

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
reminderReq[ReminderRequest]需要发布的提醒实例。
callbackAsyncCallback<number>异步回调,返回当前发布的提醒的reminderId。

示例

  let timer = {
      reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
      triggerTimeInSeconds: 10
  }
  reminderAgent.publishReminder(timer, (err, reminderId) => {
      console.log("callback, reminderId = " + reminderId);
  });

reminderAgent.publishReminder

publishReminder(reminderReq: ReminderRequest): Promise<number>

发布一个后台代理提醒,使用Promise方式实现异步调用。

需要权限: ohos.permission.PUBLISH_AGENT_REMINDER

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
reminderReq[ReminderRequest]需要发布的提醒实例。

返回值

类型说明
Promise<number>返回提醒的reminderId。

示例

  let timer = {
      reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
      triggerTimeInSeconds: 10
  }
  reminderAgent.publishReminder(timer).then((reminderId) => {
      console.log("promise, reminderId = " + reminderId);
  });

reminderAgent.cancelReminder

cancelReminder(reminderId: number, callback: AsyncCallback<void>): void

取消指定id的提醒,使用callback方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
reminderIdnumber目标reminder的id号。
callbackAsyncCallback<void>异步回调。

示例

reminderAgent.cancelReminder(1, (err, data) => {
    console.log("cancelReminder callback");
});

reminderAgent.cancelReminder

cancelReminder(reminderId: number): Promise<void>

取消指定id的提醒,使用Promise方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
reminderIdnumber目标reminder的id号。

返回值

类型说明
Promise<void>Promise类型异步回调。

示例

reminderAgent.cancelReminder(1).then(() => {
    console.log("cancelReminder promise");
});

reminderAgent.getValidReminders

getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void

获取当前应用已设置的所有有效(未过期)的提醒,使用callback方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
callbackAsyncCallback<Array<[ReminderRequest]>>异步回调,返回当前应用已设置的所有有效(未过期)的提醒。

示例

reminderAgent.getValidReminders((err, reminders) => {
    console.log("callback, getValidReminders length = " + reminders.length);
    for (let i = 0; i < reminders.length; i++) {
        console.log("getValidReminders = " + reminders[i]);
        console.log("getValidReminders, reminderType = " + reminders[i].reminderType);
        for (let j = 0; j < reminders[i].actionButton.length; j++) {
            console.log("getValidReminders, actionButton.title = " + reminders[i].actionButton[j].title);
            console.log("getValidReminders, actionButton.type = " + reminders[i].actionButton[j].type);
        }
        console.log("getValidReminders, wantAgent.pkgName = " + reminders[i].wantAgent.pkgName);
        console.log("getValidReminders, wantAgent.abilityName = " + reminders[i].wantAgent.abilityName);
        console.log("getValidReminders, maxScreenWantAgent.pkgName = " + reminders[i].maxScreenWantAgent.pkgName);
        console.log("getValidReminders, maxScreenWantAgent.abilityName = " + reminders[i].maxScreenWantAgent.abilityName);
        console.log("getValidReminders, ringDuration = " + reminders[i].ringDuration);
        console.log("getValidReminders, snoozeTimes = " + reminders[i].snoozeTimes);
        console.log("getValidReminders, timeInterval = " + reminders[i].timeInterval);
        console.log("getValidReminders, title = " + reminders[i].title);
        console.log("getValidReminders, content = " + reminders[i].content);
        console.log("getValidReminders, expiredContent = " + reminders[i].expiredContent);
        console.log("getValidReminders, snoozeContent = " + reminders[i].snoozeContent);
        console.log("getValidReminders, notificationId = " + reminders[i].notificationId);
        console.log("getValidReminders, slotType = " + reminders[i].slotType);
    }
})

reminderAgent.getValidReminders

getValidReminders(): Promise<Array<ReminderRequest>>

获取当前应用已设置的所有有效(未过期)的提醒,使用Promise方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

返回值

类型说明
Promise<Array<[ReminderRequest]>>返回当前应用已设置的所有有效(未过期)的提醒。

示例

reminderAgent.getValidReminders().then((reminders) => {
    console.log("promise, getValidReminders length = " + reminders.length);
    for (let i = 0; i < reminders.length; i++) {
        console.log("getValidReminders = " + reminders[i]);
        console.log("getValidReminders, reminderType = " + reminders[i].reminderType);
        for (let j = 0; j < reminders[i].actionButton.length; j++) {
            console.log("getValidReminders, actionButton.title = " + reminders[i].actionButton[j].title);
            console.log("getValidReminders, actionButton.type = " + reminders[i].actionButton[j].type);
        }
        console.log("getValidReminders, wantAgent.pkgName = " + reminders[i].wantAgent.pkgName);
        console.log("getValidReminders, wantAgent.abilityName = " + reminders[i].wantAgent.abilityName);
        console.log("getValidReminders, maxScreenWantAgent.pkgName = " + reminders[i].maxScreenWantAgent.pkgName);
        console.log("getValidReminders, maxScreenWantAgent.abilityName = " + reminders[i].maxScreenWantAgent.abilityName);
        console.log("getValidReminders, ringDuration = " + reminders[i].ringDuration);
        console.log("getValidReminders, snoozeTimes = " + reminders[i].snoozeTimes);
        console.log("getValidReminders, timeInterval = " + reminders[i].timeInterval);
        console.log("getValidReminders, title = " + reminders[i].title);
        console.log("getValidReminders, content = " + reminders[i].content);
        console.log("getValidReminders, expiredContent = " + reminders[i].expiredContent);
        console.log("getValidReminders, snoozeContent = " + reminders[i].snoozeContent);
        console.log("getValidReminders, notificationId = " + reminders[i].notificationId);
        console.log("getValidReminders, slotType = " + reminders[i].slotType);
    }
})

reminderAgent.cancelAllReminders

cancelAllReminders(callback: AsyncCallback<void>): void

取消当前应用所有的提醒,使用callback方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
callbackAsyncCallback<void>异步回调。

示例

reminderAgent.cancelAllReminders((err, data) =>{
    console.log("cancelAllReminders callback")
})

reminderAgent.cancelAllReminders

cancelAllReminders(): Promise<void>

取消当前应用所有的提醒,使用Promise方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

返回值

类型说明
Promise<void>Promise类型异步回调。

示例

reminderAgent.cancelAllReminders().then(() => {
    console.log("cancelAllReminders promise")
})

reminderAgent.addNotificationSlot

addNotificationSlot(slot: NotificationSlot, callback: AsyncCallback<void>): void

添加一个NotificationSlot,使用callback方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
slot[NotificationSlot]notification slot实例,仅支持设置其type属性。
callbackAsyncCallback<void>异步回调。

示例

import notification from '@ohos.notification'

let mySlot = {
    type: notification.SlotType.SOCIAL_COMMUNICATION
}
reminderAgent.addNotificationSlot(mySlot, (err, data) => {
    console.log("addNotificationSlot callback");
});

reminderAgent.addNotificationSlot

addNotificationSlot(slot: NotificationSlot): Promise<void>

添加一个NotificationSlot,使用Promise方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
slot[NotificationSlot]notification slot实例,仅支持设置其type属性。

返回值

类型说明
Promise<void>Promise类型异步回调。

示例

import notification from '@ohos.notification'

let mySlot = {
    type: notification.SlotType.SOCIAL_COMMUNICATION
}
reminderAgent.addNotificationSlot(mySlot).then(() => {
   console.log("addNotificationSlot promise");
});

reminderAgent.removeNotificationSlot

removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback<void>): void

删除目标NotificationSlot,使用callback方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
slotType[notification.SlotType]目标notification slot的类型。
callbackAsyncCallback<void>异步回调。

示例

import notification from '@ohos.notification'

reminderAgent.removeNotificationSlot(notification.SlotType.CONTENT_INFORMATION, (err, data) => {
    console.log("removeNotificationSlot callback");
});

reminderAgent.removeNotificationSlot

removeNotificationSlot(slotType: notification.SlotType): Promise<void>

删除目标NotificationSlot,使用Promise方式实现异步调用。

系统能力: SystemCapability.Notification.ReminderAgent

参数

参数名类型必填说明
slotType[notification.SlotType]目标notification slot的类型。

返回值

类型说明
Promise<void>Promise类型异步回调。

示例

import notification from '@ohos.notification'

reminderAgent.removeNotificationSlot(notification.SlotType.CONTENT_INFORMATION).then(() => {
    console.log("removeNotificationSlot promise");
});

ActionButtonType

按钮的类型。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称默认值说明
ACTION_BUTTON_TYPE_CLOSE0表示关闭提醒的按钮。
ACTION_BUTTON_TYPE_SNOOZE1表示延迟提醒的按钮。

ReminderType

提醒的类型。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称默认值说明
REMINDER_TYPE_TIMER0表示提醒类型:倒计时。
REMINDER_TYPE_CALENDAR1表示提醒类型:日历。
REMINDER_TYPE_ALARM2表示提醒类型:闹钟。

ActionButton

用于设置弹出的提醒通知信息上显示的按钮类型和标题。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
titlestring按钮显示的标题。
type[ActionButtonType]按钮的类型。

WantAgent

点击提醒通知后跳转的目标ability信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
pkgNamestring指明点击提醒通知栏后跳转的目标hap包名。
abilityNamestring指明点击提醒通知栏后跳转的目标ability名称。

MaxScreenWantAgent

提醒到达时自动拉起的目标ability信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
pkgNamestring指明提醒到达时自动拉起的目标hap包名(如果设备在使用中,则只弹出通知横幅框)。
abilityNamestring指明提醒到达时自动拉起的目标ability名(如果设备在使用中,则只弹出通知横幅框)。

ReminderRequest

提醒实例对象,用于设置提醒类型、响铃时长等具体信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
reminderTypeReminderType指明提醒类型。
actionButton[ActionButton?, ActionButton?]弹出的提醒通知栏中显示的按钮(参数可选,支持0/1/2个按钮)。
wantAgentWantAgent点击通知后需要跳转的目标ability信息。
maxScreenWantAgentMaxScreenWantAgent提醒到达时跳转的目标包。如果设备正在使用中,则弹出一个通知框。
ringDurationnumber指明响铃时长。
snoozeTimesnumber指明延迟提醒次数。
timeIntervalnumber执行延迟提醒间隔。
titlestring指明提醒标题。
contentstring指明提醒内容。
expiredContentstring指明提醒过期后需要显示的内容。
snoozeContentstring指明延迟提醒时需要显示的内容。
notificationIdnumber指明提醒使用的通知的id号,相同id号的提醒会覆盖。
slotType[notification.SlotType]指明提醒的slot类型。

ReminderRequestCalendar

ReminderRequestCalendar extends ReminderRequest

日历实例对象,用于设置提醒的时间。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
dateTime[LocalDateTime]指明提醒的目标时间。
repeatMonthsArray<number>指明重复提醒的月份。
repeatDaysArray<number>指明重复提醒的日期。

ReminderRequestAlarm

ReminderRequestAlarm extends ReminderRequest

闹钟实例对象,用于设置提醒的时间。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
hournumber指明提醒的目标时刻。
minutenumber指明提醒的目标分钟。
daysOfWeekArray<number>指明每周哪几天需要重复提醒。

ReminderRequestTimer

ReminderRequestTimer extends ReminderRequest

倒计时实例对象,用于设置提醒的时间。

系统能力:SystemCapability.Notification.ReminderAgent

名称参数类型必填说明 HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
triggerTimeInSecondsnumber指明倒计时的秒数。

搜狗高速浏览器截图20240326151547.png

LocalDateTime

用于日历类提醒设置时指定时间信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent

名称参数类型必填说明
yearnumber
monthnumber
daynumber
hournumber
minutenumber
secondnumber

鸿蒙Next核心技术分享

1、鸿蒙基础知识←《鸿蒙NEXT星河版开发学习文档》

2、鸿蒙ArkUI←《鸿蒙NEXT星河版开发学习文档》

3、鸿蒙进阶技术←《鸿蒙NEXT星河版开发学习文档》

 4、鸿蒙就业高级技能←《鸿蒙NEXT星河版开发学习文档》 

 5、鸿蒙多媒体技术←《鸿蒙NEXT星河版开发学习文档》 

6、鸿蒙南向驱动开发←《鸿蒙NEXT星河版开发学习文档》  

7、鸿蒙南向内核设备开发←《鸿蒙NEXT星河版开发学习文档》  

 8、鸿蒙系统裁剪与移植←《鸿蒙NEXT星河版开发学习文档》  

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值