• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Agent-Powered Scheduled Reminder Development
2
3## When to Use
4
5You can set your application to call the **ReminderRequest** class to create scheduled reminders for countdown timers, calendar events, and alarm clocks. When the created reminders are published, the timing and pop-up notification functions of your application will be taken over by the reminder agent in the background, even when your application is frozen or exits.
6
7
8## Available APIs
9
10**reminderAgent** encapsulates the APIs for publishing and canceling reminders.
11
12For details about the APIs, see [reminderAgent](../reference/apis/js-apis-reminderAgent.md).
13
14**Table 1** Major APIs in reminderAgent
15
16| Name| Description|
17| -------- | -------- |
18| publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback&lt;number&gt;): void<br>publishReminder(reminderReq: ReminderRequest): Promise&lt;number&gt; | Publishes a scheduled reminder.<br>The maximum number of valid notifications (excluding expired ones that will not pop up again) is 30 for one application and 2000 for the entire system. |
19| cancelReminder(reminderId: number, callback: AsyncCallback&lt;void&gt;): void<br>cancelReminder(reminderId: number): Promise&lt;void&gt; | Cancels a specified reminder. (The value of **reminderId** is obtained from the return value of **publishReminder**.)|
20| getValidReminders(callback: AsyncCallback&lt;Array&lt;ReminderRequest&gt;&gt;): void<br>getValidReminders(): Promise&lt;Array&lt;ReminderRequest&gt;&gt; | Obtains all valid reminders set by the current application.|
21| cancelAllReminders(callback: AsyncCallback&lt;void&gt;): void<br>cancelAllReminders(): Promise&lt;void&gt; | Cancels all reminders set by the current application.|
22| addNotificationSlot(slot: NotificationSlot, callback: AsyncCallback&lt;void&gt;): void<br>addNotificationSlot(slot: NotificationSlot): Promise&lt;void&gt; | Registers a **NotificationSlot** instance to be used by the reminder.|
23| removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback&lt;void&gt;): void<br>removeNotificationSlot(slotType: notification.SlotType): Promise&lt;void&gt; | Removes a **NotificationSlot** instance of a specified type.|
24
25## How to Develop
26
27> **NOTE**
28>
29> 1. To publish a reminder through the reminder agent, your application needs to apply for the **ohos.permission.PUBLISH_AGENT_REMINDER** permission.
30>
31> 2. Your application must have notification enabled. For details, see [Notification.requestEnableNotification](../reference/apis/js-apis-notification.md#notificationrequestenablenotification8).
32
331. Define a reminder agent.
34
35   Sample code for defining a reminder agent for a countdown timer:
36   ```js
37   import reminderAgent from '@ohos.reminderAgent';
38   import notification from '@ohos.notification';
39   export default {
40       // For a JS project:
41       // timer: {
42       // For an eTS project:
43       let timer : reminderAgent.ReminderRequestTimer = {
44           reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
45           triggerTimeInSeconds: 10,
46           actionButton: [
47               {
48                   title: "close",
49                   type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
50               }
51           ],
52           wantAgent: {
53               pkgName: "com.example.device",
54               abilityName: "com.example.device.MainAbility"
55           },
56           maxScreenWantAgent: {
57               pkgName: "com.example.device",
58               abilityName: "com.example.device.MainAbility"
59           },
60           title: "this is title",
61           content: "this is content",
62           expiredContent: "this reminder has expired",
63           notificationId: 100,
64           slotType: notification.SlotType.SOCIAL_COMMUNICATION
65       }
66   }
67   ```
68
69   Sample code for defining a reminder agent for a calendar event:
70
71    ```js
72    // For a JS project:
73    // calendar: {
74    // For an eTS project:
75    let calendar : reminderAgent.ReminderRequestCalendar = {
76        reminderType: reminderAgent.ReminderType.REMINDER_TYPE_CALENDAR,
77        dateTime: {
78            year: 2050,
79            month: 7,
80            day: 30,
81            hour: 11,
82            minute: 14,
83            second: 30
84        },
85        repeatMonths: [1],
86        repeatDays: [1],
87        actionButton: [
88            {
89                title: "close",
90                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
91            },
92            {
93                title: "snooze",
94                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
95            },
96        ],
97        wantAgent: {
98            pkgName: "com.example.device",
99            abilityName: "com.example.device.MainAbility"
100        },
101        maxScreenWantAgent: {
102            pkgName: "com.example.device",
103            abilityName: "com.example.device.MainAbility"
104        },
105        ringDuration: 5,
106        snoozeTimes: 2,
107        timeInterval: 5,
108        title: "this is title",
109        content: "this is content",
110        expiredContent: "this reminder has expired",
111        snoozeContent: "remind later",
112        notificationId: 100,
113        slotType: notification.SlotType.SOCIAL_COMMUNICATION
114    }
115    ```
116
117    Sample code for defining a reminder agent for an alarm:
118
119    ```js
120    // For a JS project:
121    // alarm: {
122    // For an eTS project:
123    let alarm : reminderAgent.ReminderRequestAlarm = {
124        reminderType: reminderAgent.ReminderType.REMINDER_TYPE_ALARM,
125        hour: 11,
126        minute: 14,
127        daysOfWeek: [0],
128        actionButton: [
129            {
130                title: "close",
131                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
132            },
133            {
134                title: "snooze",
135                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
136            },
137        ],
138        wantAgent: {
139            pkgName: "com.example.device",
140            abilityName: "com.example.device.MainAbility"
141        },
142        maxScreenWantAgent: {
143            pkgName: "com.example.device",
144            abilityName: "com.example.device.MainAbility"
145        },
146        ringDuration: 5,
147        snoozeTimes: 2,
148        timeInterval: 5,
149        title: "this is title",
150        content: "this is content",
151        expiredContent: "this reminder has expired",
152        snoozeContent: "remind later",
153        notificationId: 100,
154        slotType: notification.SlotType.SOCIAL_COMMUNICATION
155    }
156    ```
157
1582. Publish a countdown reminder.
159   ```js
160   startTimer() {
161       reminderAgent.publishReminder(this.timer, (err, reminderId) =>{
162           this.printInfo(JSON.stringify(err));
163           this.printInfo("reminderId:" + reminderId);
164       });
165   }
166   ```
167
168   HML page code:
169   ```html
170   <div class="container">
171       <button type="text" value="publishReminder" onclick="startTimer"></button>
172   </div>
173   ```
174
175