• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Agent-powered Reminder (ArkTS)
2
3## Overview
4
5### Introduction
6
7After an application switches to the background or an application process is terminated, the system can still send scheduled notifications on behalf of the application through reminderAgentManager. When the application switches to the background or the process is terminated, the system sends reminders on behalf of the application. Currently, the following reminder types are supported: timer, calendar, and alarm.<!--RP1--><!--RP1End-->
8
9- Timer: reminders based on countdown timers
10
11- Calendar: reminders based on calendar events
12
13- Alarm: reminders based on alarm clocks
14
15### Constraints
16
17<!--RP2-->
18- **Maximum reminders**: 30 for regular applications, 10,000 for system applications, and 12,000 total for the system.
19<!--RP2End-->
20
21> **NOTE**
22>
23> When the reminder time arrives, the notification center displays the relevant reminder. The reminder remains active and unexpired unless the user touches the CLOSE button, at which point the reminder becomes expired.
24>
25> For a recurring reminder (for example, a daily reminder), the reminder is always valid regardless of whether the user touches the CLOSE button.
26
27- **Redirection limit**: The application that is redirected to upon a click on the notification must be the application that requested the agent-powered reminder.
28
29<!--RP3--><!--RP3End-->
30
31## Relationship with Other Kits
32- Notifications are published using Notification Kit. For notification styles, see [Notification Style](../notification/notification-overview.md#notification-style).
33
34## Available APIs
35
36**Table 1** Main APIs for agent-powered reminders
37
38The table below uses promise as an example to describe the APIs used for developing agent-powered reminders. For details about more APIs and their usage, see [reminderAgentManager](../reference/apis-backgroundtasks-kit/js-apis-reminderAgentManager.md).
39| API| Description|
40| -------- | -------- |
41| publishReminder(reminderReq: ReminderRequest): Promise&lt;number&gt; | Publishes a scheduled reminder.|
42| cancelReminder(reminderId: number): Promise&lt;void&gt; | Cancels a reminder.|
43| getValidReminders(): Promise&lt;Array&lt;ReminderRequest&gt;&gt; | Obtains all valid reminders set by the current application.|
44| cancelAllReminders(): Promise&lt;void&gt; | Cancels all reminders set by the current application.|
45| addNotificationSlot(slot: NotificationSlot): Promise&lt;void&gt; | Adds a notification slot.|
46| removeNotificationSlot(slotType: notification.SlotType): Promise&lt;void&gt; | Removes a notification slot.|
47
48
49## How to Develop
50
511. Declare the **ohos.permission.PUBLISH_AGENT_REMINDER** permission. For details, see [Declaring Permissions](../security/AccessToken/declare-permissions.md).
52
532. [Request notification authorization](../notification/notification-enable.md). Agent-powered reminders can be used only after being authorized by the user.
54
553. Import the modules.
56
57   ```ts
58   import { reminderAgentManager } from '@kit.BackgroundTasksKit';
59   import { notificationManager } from '@kit.NotificationKit';
60   import { BusinessError } from '@kit.BasicServicesKit';
61   ```
62
634. Define a reminder. You can define the following types of reminders based on project requirements.
64
65   - Timer
66
67      ```ts
68      let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = {
69        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,   // The reminder type is timer.
70        triggerTimeInSeconds: 10,
71        actionButton: [ // Set the button type and title displayed for the reminder in the notification panel.
72          {
73            title: 'close',
74            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
75          }
76        ],
77        wantAgent: {     // Information about the target UIAbility that is displayed after the reminder notification is touched.
78          pkgName: 'com.example.myapplication',
79          abilityName: 'EntryAbility'
80        },
81        title: 'this is title', // Reminder title.
82        content: 'this is content', // Reminder content.
83        expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires.
84        notificationId: 100, // Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one.
85        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder.
86      }
87      ```
88
89   - Calendar
90
91      ```ts
92      let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = {
93        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // The reminder type is calendar.
94        dateTime: {   // Reminder time.
95          year: 2023,
96          month: 1,
97          day: 1,
98          hour: 11,
99          minute: 14,
100          second: 30
101        },
102        repeatMonths: [1], // Month in which the reminder repeats.
103        repeatDays: [1], // Date on which the reminder repeats.
104        actionButton: [ // Set the button type and title displayed for the reminder in the notification panel.
105          {
106            title: 'close',
107            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
108          },
109          {
110            title: 'snooze',
111            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
112          },
113        ],
114        wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched.
115          pkgName: 'com.example.myapplication',
116          abilityName: 'EntryAbility'
117        },
118        ringDuration: 5, // Ringing duration, in seconds.
119        snoozeTimes: 2, // Number of reminder snooze times.
120        timeInterval: 5*60, // Reminder snooze interval, in seconds.
121        title: 'this is title', // Reminder title.
122        content: 'this is content', // Reminder content.
123        expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires.
124        snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed.
125        notificationId: 100, // Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one.
126        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder.
127      }
128      ```
129
130   - Alarm
131
132      ```ts
133      let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = {
134        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // The reminder type is alarm.
135        hour: 23, // Hour portion of the reminder time.
136        minute: 9, // Minute portion of the reminder time.
137        daysOfWeek: [2], // Days of a week when the reminder repeats..
138        actionButton: [ // Set the button type and title displayed for the reminder in the notification panel.
139          {
140            title: 'close',
141            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
142          },
143          {
144            title: 'snooze',
145            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
146          },
147        ],
148        wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched.
149          pkgName: 'com.example.myapplication',
150          abilityName: 'EntryAbility'
151        },
152        ringDuration: 5, // Ringing duration, in seconds.
153        snoozeTimes: 2, // Number of reminder snooze times.
154        timeInterval: 5*60, // Reminder snooze interval, in seconds.
155        title: 'this is title', // Reminder title.
156        content: 'this is content', // Reminder content.
157        expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires.
158        snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed.
159        notificationId: 99, // Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one.
160        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder.
161      }
162      ```
163
1645. Publish the reminder. After the reminder is published, your application can use the agent-powered reminder feature.
165
166   ```ts
167    reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
168      console.info('Succeeded in publishing reminder. ');
169      let reminderId: number = res; // ID of the published reminder.
170    }).catch((err: BusinessError) => {
171      console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
172    })
173   ```
174
1756. Delete the reminder as required.
176
177   ```ts
178    let reminderId: number = 1;
179    // The reminder ID is obtained from the callback after the reminder is published.
180    reminderAgentManager.cancelReminder(reminderId).then(() => {
181      console.log('Succeeded in canceling reminder.');
182    }).catch((err: BusinessError) => {
183      console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`);
184    });
185   ```
186
187