• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 后台代理提醒开发指导
2
3## 场景介绍
4
5后台代理提醒主要提供后台提醒发布接口,开发者在应用开发时,可以调用这些接口去创建定时提醒,包括倒计时、日历、闹钟三种提醒类型。使用后台代理提醒能力后,应用可以被冻结或退出,计时和弹出提醒的功能将被后台系统服务代理。
6
7
8## 接口说明
9
10reminderAgent:封装了发布、取消提醒类通知的方法。
11
12具体后台提醒相关功能接口请见[后台代理提醒](../reference/apis/js-apis-reminderAgent.md)。
13
14**表1** reminderAgent主要接口
15
16| 接口名 | 描述 |
17| -------- | -------- |
18| publishReminder(reminderReq:&nbsp;ReminderRequest,&nbsp;callback:&nbsp;AsyncCallback&lt;number&gt;):&nbsp;void<br/>publishReminder(reminderReq:&nbsp;ReminderRequest):&nbsp;Promise&lt;number&gt; | 发布一个定时提醒类通知。<br/>单个应用有效的提醒个数最多支持30个(不包括已经超时,即后续不会再提醒的提醒实例)<br/>整个系统有效的提醒个数最多支持2000个(不包括已经超时,即后续不会再提醒的提醒实例) |
19| cancelReminder(reminderId:&nbsp;number,&nbsp;callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void<br/>cancelReminder(reminderId:&nbsp;number):&nbsp;Promise&lt;void&gt; | 取消一个指定的提醒类通知。(reminderId从publishReminder的返回值获取) |
20| getValidReminders(callback:&nbsp;AsyncCallback&lt;Array&lt;ReminderRequest&gt;&gt;):&nbsp;void<br/>getValidReminders():&nbsp;Promise&lt;Array&lt;ReminderRequest&gt;&gt; | 获取当前应用设置的所有有效的提醒。 |
21| cancelAllReminders(callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void<br/>cancelAllReminders():&nbsp;Promise&lt;void&gt; | 取消当前应用设置的所有提醒 |
22| addNotificationSlot(slot:&nbsp;NotificationSlot,&nbsp;callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void<br/>addNotificationSlot(slot:&nbsp;NotificationSlot):&nbsp;Promise&lt;void&gt; | 注册一个提醒类需要使用的NotificationSlot |
23| removeNotificationSlot(slotType:&nbsp;notification.SlotType,&nbsp;callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void<br/>removeNotificationSlot(slotType:&nbsp;notification.SlotType):&nbsp;Promise&lt;void&gt; | 删除指定类型的NotificationSlot |
24
25## 开发步骤
26
27> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
28>
29> 1. 应用需要配置权限:ohos.permission.PUBLISH_AGENT_REMINDER30>
31> 2. 应用需要申请通知弹窗:[Notification.requestEnableNotification](../reference/apis/js-apis-notification.md#notificationrequestenablenotification8)。
32
331. 定义一个提醒代理。
34
35   倒计时实例定义:
36   ```js
37   import reminderAgent from '@ohos.reminderAgent';
38   import notification from '@ohos.notification';
39   export default {
40       // eTS工程:
41       let timer : reminderAgent.ReminderRequestTimer = {
42           reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
43           triggerTimeInSeconds: 10,
44           actionButton: [
45               {
46                   title: "close",
47                   type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
48               }
49           ],
50           wantAgent: {
51               pkgName: "com.example.device",
52               abilityName: "com.example.device.MainAbility"
53           },
54           maxScreenWantAgent: {
55               pkgName: "com.example.device",
56               abilityName: "com.example.device.MainAbility"
57           },
58           title: "this is title",
59           content: "this is content",
60           expiredContent: "this reminder has expired",
61           notificationId: 100,
62           slotType: notification.SlotType.SOCIAL_COMMUNICATION
63       }
64   }
65   ```
66
67   日历实例定义:
68
69    ```js
70    // eTS工程:
71    let calendar : reminderAgent.ReminderRequestCalendar = {
72        reminderType: reminderAgent.ReminderType.REMINDER_TYPE_CALENDAR,
73        dateTime: {
74            year: 2050,
75            month: 7,
76            day: 30,
77            hour: 11,
78            minute: 14,
79            second: 30
80        },
81        repeatMonths: [1],
82        repeatDays: [1],
83        actionButton: [
84            {
85                title: "close",
86                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
87            },
88            {
89                title: "snooze",
90                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
91            },
92        ],
93        wantAgent: {
94            pkgName: "com.example.device",
95            abilityName: "com.example.device.MainAbility"
96        },
97        maxScreenWantAgent: {
98            pkgName: "com.example.device",
99            abilityName: "com.example.device.MainAbility"
100        },
101        ringDuration: 5,
102        snoozeTimes: 2,
103        timeInterval: 5,
104        title: "this is title",
105        content: "this is content",
106        expiredContent: "this reminder has expired",
107        snoozeContent: "remind later",
108        notificationId: 100,
109        slotType: notification.SlotType.SOCIAL_COMMUNICATION
110    }
111    ```
112
113    闹钟实例定义:
114
115    ```js
116    // eTS工程:
117    let alarm : reminderAgent.ReminderRequestAlarm = {
118        reminderType: reminderAgent.ReminderType.REMINDER_TYPE_ALARM,
119        hour: 11,
120        minute: 14,
121        daysOfWeek: [0],
122        actionButton: [
123            {
124                title: "close",
125                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
126            },
127            {
128                title: "snooze",
129                type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
130            },
131        ],
132        wantAgent: {
133            pkgName: "com.example.device",
134            abilityName: "com.example.device.MainAbility"
135        },
136        maxScreenWantAgent: {
137            pkgName: "com.example.device",
138            abilityName: "com.example.device.MainAbility"
139        },
140        ringDuration: 5,
141        snoozeTimes: 2,
142        timeInterval: 5,
143        title: "this is title",
144        content: "this is content",
145        expiredContent: "this reminder has expired",
146        snoozeContent: "remind later",
147        notificationId: 100,
148        slotType: notification.SlotType.SOCIAL_COMMUNICATION
149    }
150    ```
151
1522. 发布倒计时提醒。
153   ```js
154   startTimer() {
155       reminderAgent.publishReminder(this.timer, (err, reminderId) =>{
156           this.printInfo(JSON.stringify(err));
157           this.printInfo("reminderId:" + reminderId);
158       });
159   }
160   ```
161
162   HML页面:
163   ```html
164   <div class="container">
165       <button type="text" value="publishReminder" onclick="startTimer"></button>
166   </div>
167   ```
168
169## 相关实例
170
171基于后台代理提醒开发,有以下相关实例可供参考:
172
173- [`AlarmClock`:后台代理提醒(eTS)(API8)](https://gitee.com/openharmony/applications_app_samples/tree/samples_monthly_0730/Notification/AlarmClock)
174
175- [`FlipClock`:翻页时钟(eTS)(API8)(Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/samples_monthly_0730/CompleteApps/FlipClock)
176
177