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