diff --git a/README.md b/README.md index 4ddc596aa1e98751b54b1c6315259f0c09619067..de6824672b6ef588632602c67293c909e8c206bf 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,8 @@ │ │ ├──NotificationContentUtil.ets // 封装各种通知的主体内容 │ │ ├──NotificationManagementUtil.ets // 封装通知列表,角标设置的接口 │ │ ├──NotificationRequestUtil.ets // 接收通知的主体内容,返回完整的通知 -│ │ └──NotificationUtil.ets // 封装允许发布通知、发布通知、关闭通知的接口 +│ │ ├──NotificationUtil.ets // 封装允许发布通知、发布通知、关闭通知的接口 +│ │ └──WantAgentUtil.ets // 封装wantAgent │ └──util │ └──Logger.ets // 日志文件 └──notification/src/main/resources // 应用静态资源目录 diff --git a/entry/src/main/ets/feature/NotificationOperations.ets b/entry/src/main/ets/feature/NotificationOperations.ets index 15f2e91baafcdd9dca4d97c7b2b72bf833cc92fd..47118651c1fb304fb0aef9754322f6985a4b9391 100644 --- a/entry/src/main/ets/feature/NotificationOperations.ets +++ b/entry/src/main/ets/feature/NotificationOperations.ets @@ -15,9 +15,11 @@ import { image } from '@kit.ImageKit'; import { notificationManager } from '@kit.NotificationKit'; -import { logger, notificationUtil, notificationContentUtil, notificationRequestUtil } from '@ohos/notification'; +import { logger, notificationUtil, notificationContentUtil, notificationRequestUtil, wantAgentUtil } from '@ohos/notification'; const TAG: string = 'Sample_Notification'; +const BUNDLE_NAME: string = 'com.samples.customnotificationbadge'; +const ABILITY_NAME: string = 'MainAbility'; // The multi line text content of a multi line notification. const MULTI_LINE_CONTENT: Array = ['line0', 'line1', 'line2', 'line3']; @@ -118,4 +120,43 @@ export default class NotificationOperations { logger.info(TAG, `publishNotificationWithWantAgent error, error = ${JSON.stringify(error)}`); } } + + publishNotificationWithButtons = async () => { + try { + if (this.basicContent !== undefined && this.basicContent !== null) { + logger.info(TAG, `publishNotificationWithButtons`); + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.notification_with_buttons')); + let actionButtons: notificationManager.NotificationActionButton[] = [ + { + title: this.context.resourceManager.getStringSync($r('app.string.first_button')), + wantAgent: await wantAgentUtil.createWantAgentForCommonEvent('') + }, + { + title: this.context.resourceManager.getStringSync($r('app.string.second_button')), + wantAgent: await wantAgentUtil.createWantAgentForStartAbility(BUNDLE_NAME, ABILITY_NAME) + } + ] + let notificationContent = notificationContentUtil.initBasicNotificationContent(this.basicContent); + let notificationRequest = notificationRequestUtil.initButtonNotificationRequest(notificationContent, actionButtons); + notificationUtil.publishNotification(notificationRequest); + } + } catch (error) { + logger.info(TAG, `publishNotificationWithButtons error, error = ${JSON.stringify(error)}`); + } + } + + publishNotificationWithWantAgent = async () => { + try { + logger.info(TAG, `publishNotificationWithWantAgent`); + if (this.basicContent !== undefined && this.basicContent !== null) { + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.basic_notification')); + let notificationWantAgent = await wantAgentUtil.createWantAgentForStartAbility(BUNDLE_NAME, ABILITY_NAME); + let notificationContent = notificationContentUtil.initBasicNotificationContent(this.basicContent); + let notificationRequest = notificationRequestUtil.initWantAgentNotificationRequest(notificationContent, notificationWantAgent); + notificationUtil.publishNotification(notificationRequest); + } + } catch (error) { + logger.info(TAG, `publishNotificationWithWantAgent error, error = ${JSON.stringify(error)}`); + } + } } \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index d174a7f753fc9fdbe7039e8b24cb2028ed693d99..11c9eb0aad21e50f3bd867e944d74d2757312403 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -71,7 +71,18 @@ { "name": "accept", "value": "accept" + }, + { + "name": "notification_with_buttons", + "value": "notification_with_buttons" + }, + { + "name": "first_button", + "value": "first_button" + }, + { + "name": "second_button", + "value": "second_button" } - ] } \ No newline at end of file diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index a2353251cb83c8fa2472140cc4feac79d73869d9..11c9eb0aad21e50f3bd867e944d74d2757312403 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -71,6 +71,18 @@ { "name": "accept", "value": "accept" + }, + { + "name": "notification_with_buttons", + "value": "notification_with_buttons" + }, + { + "name": "first_button", + "value": "first_button" + }, + { + "name": "second_button", + "value": "second_button" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index d3b7948b9a69955796041d246d3ad853571cad8c..922817d3c7a1f929574a7150ae408139611f5f21 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -71,6 +71,18 @@ { "name": "accept", "value": "允许" + }, + { + "name": "notification_with_buttons", + "value": "带按钮的通知" + }, + { + "name": "first_button", + "value": "第一个按钮" + }, + { + "name": "second_button", + "value": "第二个按钮" } ] } \ No newline at end of file diff --git a/notification/index.ets b/notification/index.ets index 5002a0c28430e38d2d683b59962d17104184edf8..eaf4894e9a232c83b9b6f0375cf8113122c5514a 100644 --- a/notification/index.ets +++ b/notification/index.ets @@ -19,6 +19,8 @@ export { notificationRequestUtil } from './src/main/ets/notification/Notificatio export { notificationContentUtil } from './src/main/ets/notification/NotificationContentUtil'; +export { wantAgentUtil } from './src/main/ets/notification/wantAgentUtil'; + export { notificationManagement, getAllNotificationsResultType diff --git a/notification/src/main/ets/notification/NotificationRequestUtil.ets b/notification/src/main/ets/notification/NotificationRequestUtil.ets index 598ca12eb6cce0bd9085e02847eb529c6bb40aaa..74671a12363575fe8a8a6536b1af3ac5ac2bad1e 100644 --- a/notification/src/main/ets/notification/NotificationRequestUtil.ets +++ b/notification/src/main/ets/notification/NotificationRequestUtil.ets @@ -14,6 +14,7 @@ */ import { notificationManager } from '@kit.NotificationKit'; +import { WantAgent } from '@ohos.wantAgent' interface NotificationRequestUtilResultType { slotType: notificationManager.SlotType, @@ -35,6 +36,41 @@ class NotificationRequestUtil { } return result; } + + /** + * init NotificationRequest width buttons + * @param notificationContent + * @param notificationActionButtons + * @return return the created NotificationRequest + */ + initButtonNotificationRequest(notificationContent: notificationManager.NotificationContent, notificationActionButtons: notificationManager.NotificationActionButton[]): notificationManager.NotificationRequest { + let actionButtons = notificationActionButtons + if (notificationActionButtons.length > 2) { // 当前通知接口最大允许有两个按钮,超过两个按钮不展示 + actionButtons = notificationActionButtons.splice(0, 2) + } + return { + notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION, + id: 1, // 通知id,默认为1 + content: notificationContent, + actionButtons: actionButtons + }; + } + + /** + * init wantAgent NotificationRequest + * @param notificationContent + * @param notificationWantAgent + * @return return the created NotificationRequest + */ + + initWantAgentNotificationRequest(notificationContent: notificationManager.NotificationContent, notificationWantAgent: WantAgent): notificationManager.NotificationRequest { + return { + notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION, + id: 1, // 通知id,默认为1 + content: notificationContent, + wantAgent: notificationWantAgent + }; + } } export let notificationRequestUtil = new NotificationRequestUtil(); \ No newline at end of file diff --git a/notification/src/main/ets/notification/WantAgentUtil.ets b/notification/src/main/ets/notification/WantAgentUtil.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ad6f1532423cf1c436af8715bcb596421c6b747 --- /dev/null +++ b/notification/src/main/ets/notification/WantAgentUtil.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { wantAgent } from '@kit.AbilityKit' + +const REQUEST_CODE: number = 0 +class WantAgentUtil { + /** + * create wantAgent for start ability + * + * @param bundleName + * @param abilityName + * @return return the created WantAgent object. + */ + async createWantAgentForStartAbility(bundleName: string, abilityName: string) { + let wantAgentInfo: wantAgent.WantAgentInfo = { + wants: [ + { + bundleName: bundleName, + abilityName: abilityName + } + ], + actionType: wantAgent.OperationType.START_ABILITY, + requestCode: REQUEST_CODE + } + return await wantAgent.getWantAgent(wantAgentInfo); + } + + /** + * create wantAgent for common event + * + * @param mAction + * @return return the created WantAgent object. + */ + async createWantAgentForCommonEvent(action: string) { + let wantAgentInfo: wantAgent.WantAgentInfo = { + wants: [{ action: action }], + actionType: wantAgent.OperationType.SEND_COMMON_EVENT, + requestCode: REQUEST_CODE + } + return await wantAgent.getWantAgent(wantAgentInfo); + } +} + +export let wantAgentUtil = new WantAgentUtil(); \ No newline at end of file