• 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, it may have scheduled tasks for reminding users, for example, flash sale reminders for shopping applications. To meet this requirement, the system provides agent-powered reminders (implemented by **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- **Quantity limit**: A third-party application supports a maximum of 30 valid reminders.<!--Del--> A system application supports a maximum of 10,000 valid reminders. The entire system supports a maximum of 12,000 valid reminders.<!--DelEnd-->
18
19> **NOTE**
20>
21> 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.
22>
23> For a recurring reminder (for example, a daily reminder), the reminder is always valid regardless of whether the user touches the CLOSE button.
24
25- **Redirection limit**: The application that is redirected to upon a click on the notification must be the application that requested the agent-powered reminder.
26
27<!--RP2--><!--RP2End-->
28
29
30## Available APIs
31
32**Table 1** Main APIs for agent-powered reminders
33
34The 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).
35| API| Description|
36| -------- | -------- |
37| publishReminder(reminderReq: ReminderRequest): Promise&lt;number&gt; | Publishes a scheduled reminder.|
38| cancelReminder(reminderId: number): Promise&lt;void&gt; | Cancels a reminder.|
39| getValidReminders(): Promise&lt;Array&lt;ReminderRequest&gt;&gt; | Obtains all valid reminders set by the current application.|
40| cancelAllReminders(): Promise&lt;void&gt; | Cancels all reminders set by the current application.|
41| addNotificationSlot(slot: NotificationSlot): Promise&lt;void&gt; | Adds a notification slot.|
42| removeNotificationSlot(slotType: notification.SlotType): Promise&lt;void&gt; | Removes a notification slot.|
43
44
45## How to Develop
46
471. Declare the **ohos.permission.PUBLISH_AGENT_REMINDER** permission. For details, see [Declaring Permissions](../security/AccessToken/declare-permissions.md).
48
492. [Request notification authorization](../notification/notification-enable.md). Agent-powered reminders can be used only after being authorized by the user.
50
513. Import the modules.
52
53   ```ts
54   import { reminderAgentManager } from '@kit.BackgroundTasksKit';
55   import { notificationManager } from '@kit.NotificationKit';
56   import { BusinessError } from '@kit.BasicServicesKit';
57   ```
58
594. Define a reminder. You can define the following types of reminders based on project requirements.
60
61   - Timer
62
63      ```ts
64      let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = {
65        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,   // The reminder type is timer.
66        triggerTimeInSeconds: 10,
67        actionButton: [ // Set the button type and title displayed for the reminder in the notification panel.
68          {
69            title: 'close',
70            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
71          }
72        ],
73        wantAgent: {     // Information about the target UIAbility that is displayed after the reminder notification is touched.
74          pkgName: 'com.example.myapplication',
75          abilityName: 'EntryAbility'
76        },
77        title: 'this is title', // Reminder title.
78        content: 'this is content', // Reminder content.
79        expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires.
80        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.
81        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder.
82      }
83      ```
84
85   - Calendar
86
87      ```ts
88      let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = {
89        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // The reminder type is calendar.
90        dateTime: {   // Reminder time.
91          year: 2023,
92          month: 1,
93          day: 1,
94          hour: 11,
95          minute: 14,
96          second: 30
97        },
98        repeatMonths: [1], // Month in which the reminder repeats.
99        repeatDays: [1], // Date on which the reminder repeats.
100        actionButton: [ // Set the button type and title displayed for the reminder in the notification panel.
101          {
102            title: 'close',
103            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
104          },
105          {
106            title: 'snooze',
107            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
108          },
109        ],
110        wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched.
111          pkgName: 'com.example.myapplication',
112          abilityName: 'EntryAbility'
113        },
114        ringDuration: 5, // Ringing duration, in seconds.
115        snoozeTimes: 2, // Number of reminder snooze times.
116        timeInterval: 5*60, // Reminder snooze interval, in seconds.
117        title: 'this is title', // Reminder title.
118        content: 'this is content', // Reminder content.
119        expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires.
120        snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed.
121        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.
122        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder.
123      }
124      ```
125
126   - Alarm
127
128      ```ts
129      let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = {
130        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // The reminder type is alarm.
131        hour: 23, // Hour portion of the reminder time.
132        minute: 9, // Minute portion of the reminder time.
133        daysOfWeek: [2], // Days of a week when the reminder repeats..
134        actionButton: [ // Set the button type and title displayed for the reminder in the notification panel.
135          {
136            title: 'close',
137            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
138          },
139          {
140            title: 'snooze',
141            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
142          },
143        ],
144        wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched.
145          pkgName: 'com.example.myapplication',
146          abilityName: 'EntryAbility'
147        },
148        ringDuration: 5, // Ringing duration, in seconds.
149        snoozeTimes: 2, // Number of reminder snooze times.
150        timeInterval: 5*60, // Reminder snooze interval, in seconds.
151        title: 'this is title', // Reminder title.
152        content: 'this is content', // Reminder content.
153        expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires.
154        snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed.
155        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.
156        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder.
157      }
158      ```
159
1605. Publish the reminder. After the reminder is published, your application can use the agent-powered reminder feature.
161
162   ```ts
163    reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
164      console.info('Succeeded in publishing reminder. ');
165      let reminderId: number = res; // ID of the published reminder.
166    }).catch((err: BusinessError) => {
167      console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
168    })
169   ```
170
1716. Delete the reminder as required.
172
173   ```ts
174    let reminderId: number = 1;
175    // The reminder ID is obtained from the callback after the reminder is published.
176    reminderAgentManager.cancelReminder(reminderId).then(() => {
177      console.log('Succeeded in canceling reminder.');
178    }).catch((err: BusinessError) => {
179      console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`);
180    });
181   ```
182
183