• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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
16import notification from '@ohos.notificationManager'
17import { logger } from '../util/Logger'
18
19const TAG: string = 'NotificationUtilModel'
20
21class NotificationUtil {
22  /**
23   * enable notification
24   */
25  async enableNotification() {
26    try {
27      await notification.requestEnableNotification()
28      logger.info(TAG, `enableNotification success`)
29    } catch (err) {
30      logger.info(TAG, `enableNotification err ${JSON.stringify(err)}`)
31    }
32  }
33
34  /**
35   *
36   * @param notificationRequest
37   * @param id, Support specifying notification id when publishing notifications
38   */
39  async publishNotification(notificationRequest: notification.NotificationRequest, id?: number) {
40    if (id && id > 0) {
41      notificationRequest.id = id
42    }
43    try {
44      await notification.publish(notificationRequest)
45      logger.info(TAG, `publish notification success`)
46    } catch (err) {
47      if (err) {
48        logger.info(TAG, `publishNotification err ${JSON.stringify(err)}`)
49      }
50    }
51  }
52
53  /**
54   * cancel notification by id
55   */
56  async cancelNotificationById(id: number) {
57    try {
58      await notification.cancel(id)
59      logger.info(TAG, `cancel success`)
60    } catch (err) {
61      if (err) {
62        logger.info(TAG, `cancel err ${JSON.stringify(err)}`)
63      }
64    }
65  }
66
67  /**
68   * cancel all notification
69   */
70  async cancelAllNotifications() {
71    try {
72      await notification.cancelAll()
73      logger.info(TAG, `cancel all success`)
74    } catch (err) {
75      if (err) {
76        logger.info(TAG, `cancel all err ${JSON.stringify(err)}`)
77      }
78    }
79  }
80}
81
82export let notificationUtil = new NotificationUtil()
83