• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License"),
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @file Provides methods that will be called back when the subscriber receives a new notification or a notification is canceled
18 * @kit NotificationKit
19 */
20
21import { NotificationRequest } from './notificationRequest';
22import { NotificationSortingMap } from './notificationSortingMap';
23import notification from '../@ohos.notification';
24import type notificationManager from '../@ohos.notificationManager';
25
26/**
27 * Provides methods that will be called back when the subscriber receives a new notification or
28 * a notification is canceled.
29 *
30 * @interface NotificationSubscriber
31 * @syscap SystemCapability.Notification.Notification
32 * @systemapi
33 * @since 7
34 */
35export interface NotificationSubscriber {
36  /**
37   * The callback function that receives a new notification.
38   *
39   * @type { ?function }
40   * @syscap SystemCapability.Notification.Notification
41   * @systemapi
42   * @since 7
43   */
44  onConsume?: (data: SubscribeCallbackData) => void;
45
46  /**
47   * The callback function that cancels the notification.
48   *
49   * @type { ?function }
50   * @syscap SystemCapability.Notification.Notification
51   * @systemapi
52   * @since 7
53   */
54  onCancel?: (data: SubscribeCallbackData) => void;
55
56  /**
57   * The callback function that updates the sort of notifications.
58   *
59   * @type { ?function }
60   * @syscap SystemCapability.Notification.Notification
61   * @systemapi
62   * @since 7
63   */
64  onUpdate?: (data: NotificationSortingMap) => void;
65
66  /**
67   * The callback function of the completed subscription.
68   *
69   * @type { ?function }
70   * @syscap SystemCapability.Notification.Notification
71   * @systemapi
72   * @since 7
73   */
74  onConnect?: () => void;
75
76  /**
77   * The callback function to unsubscribe.
78   *
79   * @type { ?function }
80   * @syscap SystemCapability.Notification.Notification
81   * @systemapi
82   * @since 7
83   */
84  onDisconnect?: () => void;
85
86  /**
87   * The callback function that service disconnected.
88   *
89   * @type { ?function }
90   * @syscap SystemCapability.Notification.Notification
91   * @systemapi
92   * @since 7
93   */
94  onDestroy?: () => void;
95
96  /**
97   * Callback when the Do Not Disturb setting changed.
98   *
99   * @type { ?function }
100   * @syscap SystemCapability.Notification.Notification
101   * @systemapi
102   * @since 8
103   * @deprecated since 11
104   * @useinstead NotificationSubscriber#onDoNotDisturbChanged
105   */
106  onDoNotDisturbDateChange?: (mode: notification.DoNotDisturbDate) => void;
107
108  /**
109   * Callback when the Do Not Disturb setting changed.
110   *
111   * @type { ?function }
112   * @syscap SystemCapability.Notification.Notification
113   * @systemapi
114   * @since 11
115   */
116  onDoNotDisturbChanged?: (mode: notificationManager.DoNotDisturbDate) => void;
117
118  /**
119   * Callback when the notification permission is changed.
120   *
121   * @type { ?function }
122   * @syscap SystemCapability.Notification.Notification
123   * @systemapi
124   * @since 8
125   */
126  onEnabledNotificationChanged?: (callbackData: EnabledNotificationCallbackData) => void;
127
128  /**
129   * Callback when badge number changed.
130   *
131   * @type { ?function }
132   * @syscap SystemCapability.Notification.Notification
133   * @systemapi
134   * @since 10
135   */
136  onBadgeChanged?: (data: BadgeNumberCallbackData) => void;
137
138  /**
139   * Callback when badge enabled state changed.
140   *
141   * @type { ?BadgeEnabledChangedCallback }
142   * @syscap SystemCapability.Notification.Notification
143   * @systemapi
144   * @since 12
145   */
146  onBadgeEnabledChanged?: BadgeEnabledChangedCallback;
147
148  /**
149   * Callback when badge cancel notifications.
150   *
151   * @type { ?function }
152   * @syscap SystemCapability.Notification.Notification
153   * @systemapi
154   * @since 11
155   */
156  onBatchCancel?: (data: Array<SubscribeCallbackData>) => void;
157}
158
159/**
160 * Provides methods that will be called back when the subscriber receives a new notification or
161 * a notification is canceled.
162 *
163 * @typedef SubscribeCallbackData
164 * @syscap SystemCapability.Notification.Notification
165 * @systemapi
166 * @since 7
167 */
168export interface SubscribeCallbackData {
169  /**
170   * Content of the notice.
171   *
172   * @type { NotificationRequest }
173   * @readonly
174   * @syscap SystemCapability.Notification.Notification
175   * @systemapi
176   * @since 7
177   */
178  readonly request: NotificationRequest;
179
180  /**
181   * Notify sorting information.
182   *
183   * @type { ?NotificationSortingMap }
184   * @readonly
185   * @syscap SystemCapability.Notification.Notification
186   * @systemapi
187   * @since 7
188   */
189  readonly sortingMap?: NotificationSortingMap;
190
191  /**
192   * The reason for the deletion.(1:CLICK_REASON_REMOVE,2:CANCEL_REASON_REMOVE)
193   *
194   * @type { ?number }
195   * @readonly
196   * @syscap SystemCapability.Notification.Notification
197   * @systemapi
198   * @since 7
199   */
200  readonly reason?: number;
201
202  /**
203   * Notification sound.
204   *
205   * @type { ?string }
206   * @readonly
207   * @syscap SystemCapability.Notification.Notification
208   * @systemapi
209   * @since 7
210   */
211  readonly sound?: string;
212
213  /**
214   * Notice the vibration.
215   *
216   * @type { ?Array<number> }
217   * @readonly
218   * @syscap SystemCapability.Notification.Notification
219   * @systemapi
220   * @since 7
221   */
222  readonly vibrationValues?: Array<number>;
223}
224
225/**
226 * Describes the properties of the application that the permission to send notifications
227 * or the badge enabled state has changed.
228 *
229 * @typedef EnabledNotificationCallbackData
230 * @syscap SystemCapability.Notification.Notification
231 * @systemapi
232 * @since 8
233 */
234export interface EnabledNotificationCallbackData {
235  /**
236   * The bundle name of the application.
237   *
238   * @type { string }
239   * @readonly
240   * @syscap SystemCapability.Notification.Notification
241   * @systemapi
242   * @since 8
243   */
244  readonly bundle: string;
245
246  /**
247   * The uid of the application.
248   *
249   * @type { number }
250   * @readonly
251   * @syscap SystemCapability.Notification.Notification
252   * @systemapi
253   * @since 8
254   */
255  readonly uid: number;
256
257  /**
258   * Apply notification enable status.
259   *
260   * @type { boolean }
261   * @readonly
262   * @syscap SystemCapability.Notification.Notification
263   * @systemapi
264   * @since 8
265   */
266  readonly enable: boolean;
267}
268
269/**
270 * Describes the badge number of the application has changed.
271 *
272 * @typedef BadgeNumberCallbackData
273 * @syscap SystemCapability.Notification.Notification
274 * @systemapi
275 * @since 10
276 */
277export interface BadgeNumberCallbackData {
278  /**
279   * bundle name
280   *
281   * @type { string }
282   * @readonly
283   * @syscap SystemCapability.Notification.Notification
284   * @systemapi
285   * @since 10
286   */
287  readonly bundle: string;
288
289  /**
290   * The uid of the application.
291   *
292   * @type { number }
293   * @readonly
294   * @syscap SystemCapability.Notification.Notification
295   * @systemapi
296   * @since 10
297   */
298  readonly uid: number;
299
300  /**
301   * badge number
302   *
303   * @type { number }
304   * @readonly
305   * @syscap SystemCapability.Notification.Notification
306   * @systemapi
307   * @since 10
308   */
309  readonly badgeNumber: number;
310
311  /**
312   * Application instance key.
313   *
314   * @type { ?number }
315   * @readonly
316   * @syscap SystemCapability.Notification.Notification
317   * @systemapi
318   * @since 12
319   * @deprecated since 15
320   * @useinstead BadgeNumberCallbackData#appInstanceKey
321   */
322  readonly instanceKey?: number;
323
324  /**
325   * Application instance key.
326   *
327   * @type { ?string }
328   * @readonly
329   * @syscap SystemCapability.Notification.Notification
330   * @systemapi
331   * @since 15
332   */
333  readonly appInstanceKey?: string;
334}
335
336/**
337 * Defines the callback of BadgeEnabledChanged.
338 * @typedef BadgeEnabledChangedCallback
339 * @syscap SystemCapability.Notification.Notification
340 * @since 12
341 */
342export interface BadgeEnabledChangedCallback {
343  /**
344   * Defines the BadgeEnabledChanged callback.
345   * @param { EnabledNotificationCallbackData } data
346   * @syscap SystemCapability.Notification.Notification
347   * @systemapi
348   * @since 12
349   */
350  (data: EnabledNotificationCallbackData): void;
351}
352