From 3d9eec7acbe93c00f24e4cdeb33edd8156a56ea7 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Fri, 20 Jun 2025 18:35:23 +0800 Subject: [PATCH 1/4] add samples Signed-off-by: songbao1 --- README.md | 3 +- .../ets/feature/NotificationOperations.ets | 39 +++++++++++++ .../notification/NotificationRequestUtil.ets | 35 ++++++++++++ .../main/ets/notification/WantAgentUtil.ets | 57 +++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 notification/src/main/ets/notification/WantAgentUtil.ets diff --git a/README.md b/README.md index 4ddc596..de68246 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 15f2e91..1c7e83d 100644 --- a/entry/src/main/ets/feature/NotificationOperations.ets +++ b/entry/src/main/ets/feature/NotificationOperations.ets @@ -118,4 +118,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: notification.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.context !== undefined && this.context !== null && this.basicContent !== undefined && this.basicContent !== null) { + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.clickable_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/notification/src/main/ets/notification/NotificationRequestUtil.ets b/notification/src/main/ets/notification/NotificationRequestUtil.ets index 598ca12..2783333 100644 --- a/notification/src/main/ets/notification/NotificationRequestUtil.ets +++ b/notification/src/main/ets/notification/NotificationRequestUtil.ets @@ -35,6 +35,41 @@ class NotificationRequestUtil { } return result; } + + /** + * init NotificationRequest width buttons + * @param notificationContent + * @param notificationActionButtons + * @return return the created NotificationRequest + */ + initButtonNotificationRequest(notificationContent: notification.NotificationContent, notificationActionButtons: notification.NotificationActionButton[]): notification.NotificationRequest { + let actionButtons = notificationActionButtons + if (notificationActionButtons.length > 2) { // 当前通知接口最大允许有两个按钮,超过两个按钮不展示 + actionButtons = notificationActionButtons.splice(0, 2) + } + return { + slotType: notification.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: notification.NotificationContent, notificationWantAgent: WantAgent): notification.NotificationRequest { + return { + slotType: notification.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 0000000..ee261a9 --- /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 '@ohos.app.ability.wantAgent' + +const REQUEST_CODE: number = 0 // WantAgentInfo的请求码,默认定义成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 + } + ], + operationType: wantAgent.OperationType.START_ABILITY, + requestCode: REQUEST_CODE // requestCode是WantAgentInfo的请求码,是使用者定义的一个私有值 + } + 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 }], + operationType: wantAgent.OperationType.SEND_COMMON_EVENT, + requestCode: REQUEST_CODE // requestCode是WantAgentInfo的请求码,是使用者定义的一个私有值 + } + return await wantAgent.getWantAgent(wantAgentInfo); + } +} + +export let wantAgentUtil = new WantAgentUtil(); \ No newline at end of file -- Gitee From 831fef401c77eb9ed45f485f4ffe5b8ed51dcaf0 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Fri, 20 Jun 2025 18:51:39 +0800 Subject: [PATCH 2/4] add samples Signed-off-by: songbao1 --- .../src/main/ets/feature/NotificationOperations.ets | 8 +++++--- entry/src/main/resources/base/element/string.json | 13 ++++++++++++- entry/src/main/resources/en_US/element/string.json | 12 ++++++++++++ entry/src/main/resources/zh_CN/element/string.json | 12 ++++++++++++ .../ets/notification/NotificationRequestUtil.ets | 1 + 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/entry/src/main/ets/feature/NotificationOperations.ets b/entry/src/main/ets/feature/NotificationOperations.ets index 1c7e83d..b1a95f8 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']; @@ -146,8 +148,8 @@ export default class NotificationOperations { publishNotificationWithWantAgent = async () => { try { logger.info(TAG, `publishNotificationWithWantAgent`); - if (this.context !== undefined && this.context !== null && this.basicContent !== undefined && this.basicContent !== null) { - this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.clickable_notification')); + 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); diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index d174a7f..11c9eb0 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 a235325..11c9eb0 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 d3b7948..922817d 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/src/main/ets/notification/NotificationRequestUtil.ets b/notification/src/main/ets/notification/NotificationRequestUtil.ets index 2783333..6d15ff1 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, -- Gitee From 8a28427dba38a2c0a54afb44356fad3b029cd816 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Wed, 25 Jun 2025 18:40:15 +0800 Subject: [PATCH 3/4] add samples Signed-off-by: songbao1 --- entry/src/main/ets/feature/NotificationOperations.ets | 2 +- notification/index.ets | 2 ++ .../src/main/ets/notification/NotificationRequestUtil.ets | 8 ++++---- notification/src/main/ets/notification/WantAgentUtil.ets | 8 ++++---- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/entry/src/main/ets/feature/NotificationOperations.ets b/entry/src/main/ets/feature/NotificationOperations.ets index b1a95f8..4711865 100644 --- a/entry/src/main/ets/feature/NotificationOperations.ets +++ b/entry/src/main/ets/feature/NotificationOperations.ets @@ -126,7 +126,7 @@ export default class NotificationOperations { 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: notification.NotificationActionButton[] = [ + let actionButtons: notificationManager.NotificationActionButton[] = [ { title: this.context.resourceManager.getStringSync($r('app.string.first_button')), wantAgent: await wantAgentUtil.createWantAgentForCommonEvent('') diff --git a/notification/index.ets b/notification/index.ets index 5002a0c..eaf4894 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 6d15ff1..7cb1a01 100644 --- a/notification/src/main/ets/notification/NotificationRequestUtil.ets +++ b/notification/src/main/ets/notification/NotificationRequestUtil.ets @@ -43,13 +43,13 @@ class NotificationRequestUtil { * @param notificationActionButtons * @return return the created NotificationRequest */ - initButtonNotificationRequest(notificationContent: notification.NotificationContent, notificationActionButtons: notification.NotificationActionButton[]): notification.NotificationRequest { + initButtonNotificationRequest(notificationContent: notificationManager.NotificationContent, notificationActionButtons: notificationManager.NotificationActionButton[]): notificationManager.NotificationRequest { let actionButtons = notificationActionButtons if (notificationActionButtons.length > 2) { // 当前通知接口最大允许有两个按钮,超过两个按钮不展示 actionButtons = notificationActionButtons.splice(0, 2) } return { - slotType: notification.SlotType.CONTENT_INFORMATION, + notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION, id: 1, // 通知id,默认为1 content: notificationContent, actionButtons: actionButtons @@ -63,9 +63,9 @@ class NotificationRequestUtil { * @return return the created NotificationRequest */ - initWantAgentNotificationRequest(notificationContent: notification.NotificationContent, notificationWantAgent: WantAgent): notification.NotificationRequest { + initWantAgentNotificationRequest(notificationContent: notificationnotificationManager.NotificationContent, notificationWantAgent: WantAgent): notificationManager.NotificationRequest { return { - slotType: notification.SlotType.CONTENT_INFORMATION, + notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION, id: 1, // 通知id,默认为1 content: notificationContent, wantAgent: notificationWantAgent diff --git a/notification/src/main/ets/notification/WantAgentUtil.ets b/notification/src/main/ets/notification/WantAgentUtil.ets index ee261a9..dbc1768 100644 --- a/notification/src/main/ets/notification/WantAgentUtil.ets +++ b/notification/src/main/ets/notification/WantAgentUtil.ets @@ -13,9 +13,9 @@ * limitations under the License. */ -import wantAgent from '@ohos.app.ability.wantAgent' +import { wantAgent } from '@kit.AbilityKit' -const REQUEST_CODE: number = 0 // WantAgentInfo的请求码,默认定义成0 +const REQUEST_CODE: number = 0 class WantAgentUtil { /** * create wantAgent for start ability @@ -33,7 +33,7 @@ class WantAgentUtil { } ], operationType: wantAgent.OperationType.START_ABILITY, - requestCode: REQUEST_CODE // requestCode是WantAgentInfo的请求码,是使用者定义的一个私有值 + requestCode: REQUEST_CODE } return await wantAgent.getWantAgent(wantAgentInfo); } @@ -48,7 +48,7 @@ class WantAgentUtil { let wantAgentInfo: wantAgent.WantAgentInfo = { wants: [{ action: action }], operationType: wantAgent.OperationType.SEND_COMMON_EVENT, - requestCode: REQUEST_CODE // requestCode是WantAgentInfo的请求码,是使用者定义的一个私有值 + requestCode: REQUEST_CODE } return await wantAgent.getWantAgent(wantAgentInfo); } -- Gitee From e53bb27406b783597dc1f5f66f7f846cabefb8e6 Mon Sep 17 00:00:00 2001 From: songbao1 Date: Wed, 25 Jun 2025 18:44:51 +0800 Subject: [PATCH 4/4] add samples Signed-off-by: songbao1 --- .../src/main/ets/notification/NotificationRequestUtil.ets | 2 +- notification/src/main/ets/notification/WantAgentUtil.ets | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/notification/src/main/ets/notification/NotificationRequestUtil.ets b/notification/src/main/ets/notification/NotificationRequestUtil.ets index 7cb1a01..74671a1 100644 --- a/notification/src/main/ets/notification/NotificationRequestUtil.ets +++ b/notification/src/main/ets/notification/NotificationRequestUtil.ets @@ -63,7 +63,7 @@ class NotificationRequestUtil { * @return return the created NotificationRequest */ - initWantAgentNotificationRequest(notificationContent: notificationnotificationManager.NotificationContent, notificationWantAgent: WantAgent): notificationManager.NotificationRequest { + initWantAgentNotificationRequest(notificationContent: notificationManager.NotificationContent, notificationWantAgent: WantAgent): notificationManager.NotificationRequest { return { notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION, id: 1, // 通知id,默认为1 diff --git a/notification/src/main/ets/notification/WantAgentUtil.ets b/notification/src/main/ets/notification/WantAgentUtil.ets index dbc1768..4ad6f15 100644 --- a/notification/src/main/ets/notification/WantAgentUtil.ets +++ b/notification/src/main/ets/notification/WantAgentUtil.ets @@ -32,7 +32,7 @@ class WantAgentUtil { abilityName: abilityName } ], - operationType: wantAgent.OperationType.START_ABILITY, + actionType: wantAgent.OperationType.START_ABILITY, requestCode: REQUEST_CODE } return await wantAgent.getWantAgent(wantAgentInfo); @@ -47,7 +47,7 @@ class WantAgentUtil { async createWantAgentForCommonEvent(action: string) { let wantAgentInfo: wantAgent.WantAgentInfo = { wants: [{ action: action }], - operationType: wantAgent.OperationType.SEND_COMMON_EVENT, + actionType: wantAgent.OperationType.SEND_COMMON_EVENT, requestCode: REQUEST_CODE } return await wantAgent.getWantAgent(wantAgentInfo); -- Gitee