• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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';
18import Base from '@ohos.base';
19
20const BUNDLE_NAME: string = 'com.example.CustomNotificationPush';
21
22export enum ContentCallBackType {
23  CALLBACK_NOTIFICATION_CONTENT_BASIC_TEXT = 1,
24  CALLBACK_NOTIFICATION_CONTENT_LONG_TEXT = 3,
25  CALLBACK_NOTIFICATION_CONTENT_MULTILINE = 5,
26  CALLBACK_NOTIFICATION_CONTENT_PICTURE = 6,
27  CALLBACK_NOTIFICATION_CONTENT_CONVERSATION
28}
29
30const CONTENT_TYPE_COUNT = 7;
31
32class NotificationFilterUtil {
33  private disableContentTypes: Array<boolean> = new Array(CONTENT_TYPE_COUNT);
34
35  constructor() {
36    this.disableContentTypes.fill(false);
37    let bundle: notification.BundleOption = {
38      bundle: BUNDLE_NAME,
39    };
40    notification.setNotificationEnable(bundle, true).then(() => {
41      logger.info("setNotificationEnable success");
42    });
43  }
44
45  //使能和去使能通知过滤功能
46  setNotificationEnableFilter(enable: boolean) {
47    if (enable) {
48      try {
49        notification.on("checkNotification", this.OnCheckNotification);
50        this.disableContentTypes.fill(false);
51      } catch (error) {
52        logger.error(`notificationManager.on error: ${JSON.stringify(error as Base.BusinessError)}`);
53      }
54      logger.info("notificationFilter enable success");
55    }
56    else {
57      try {
58        notification.off("checkNotification");
59      } catch (error) {
60        logger.error(`notificationManager.off error: ${JSON.stringify(error as Base.BusinessError)}`);
61      }
62      logger.info("notificationFilter disable success");
63    }
64  }
65
66  setContentTypeDisable(type: ContentCallBackType, enable: boolean) {
67    this.disableContentTypes[type] = enable;
68    logger.info(`allowContentTypes ${JSON.stringify(type)}`);
69  }
70
71  // push callback
72  OnCheckNotification = (info: notification.NotificationCheckInfo) => {
73    logger.info(`OnCheckNotification info: ${JSON.stringify(info.contentType)}`);
74    if (this.disableContentTypes[info.contentType] === false) {
75      let result: notification.NotificationCheckResult = { code: 0, message: "Notification send success" };
76      return result;
77    } else {
78      let result: notification.NotificationCheckResult = { code: 1, message: "Notification send fail" };
79      return result;
80    }
81  }
82}
83
84export let notificationFilter = new NotificationFilterUtil();