From 758d02c8e01d21a92096de2268c8e36b1d145b5a Mon Sep 17 00:00:00 2001 From: yxk2026 <10178404+yxk2026@user.noreply.gitee.com> Date: Sat, 9 Aug 2025 15:19:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E5=BC=83=E6=8E=A5=E5=8F=A3=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/feature/NotificationOperations.ets | 109 ++++++++++-------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/entry/src/main/ets/feature/NotificationOperations.ets b/entry/src/main/ets/feature/NotificationOperations.ets index 4711865..e72118c 100644 --- a/entry/src/main/ets/feature/NotificationOperations.ets +++ b/entry/src/main/ets/feature/NotificationOperations.ets @@ -15,7 +15,13 @@ import { image } from '@kit.ImageKit'; import { notificationManager } from '@kit.NotificationKit'; -import { logger, notificationUtil, notificationContentUtil, notificationRequestUtil, wantAgentUtil } from '@ohos/notification'; +import { + logger, + notificationUtil, + notificationContentUtil, + notificationRequestUtil, + wantAgentUtil +} from '@ohos/notification'; const TAG: string = 'Sample_Notification'; const BUNDLE_NAME: string = 'com.samples.customnotificationbadge'; @@ -24,12 +30,13 @@ const ABILITY_NAME: string = 'MainAbility'; const MULTI_LINE_CONTENT: Array = ['line0', 'line1', 'line2', 'line3']; interface NOTIFICATION_GROUP_TYPE { - BASIC: string - LONG_TEXT: string - MULTI_LINE: string - PICTURE: string + BASIC: string, + LONG_TEXT: string, + MULTI_LINE: string, + PICTURE: string, CONVERSATION: string -} +}; + // Define notification groups for different types of notifications. const NOTIFICATION_GROUP: NOTIFICATION_GROUP_TYPE = { BASIC: 'BASIC', @@ -37,7 +44,7 @@ const NOTIFICATION_GROUP: NOTIFICATION_GROUP_TYPE = { MULTI_LINE: 'MULTI_LINE', PICTURE: 'PICTURE', CONVERSATION: 'CONVERSATION' -} +}; export default class NotificationOperations { private context: Context; @@ -47,116 +54,128 @@ export default class NotificationOperations { constructor(context: Context) { this.context = context; let notificationTitle = ''; - let notificationText = this.context.resourceManager.getStringSync($r('app.string.notification_content')); - let notificationAdditional = this.context.resourceManager.getStringSync($r('app.string.notification_additional')); + let notificationText = this.context.resourceManager.getStringSync($r('app.string.notification_content').id); + let notificationAdditional = + this.context.resourceManager.getStringSync($r('app.string.notification_additional').id); this.basicContent = { title: notificationTitle, text: notificationText, additionalText: notificationAdditional - } + }; } publishBasicNotification = () => { try { logger.info(TAG, 'publishBasicNotification'); - this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.basic_notification')); + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.basic_notification').id); let notificationContent = notificationContentUtil.initBasicNotificationContent(this.basicContent); notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent as notificationManager.NotificationContent) as notificationManager.NotificationRequest); } catch (error) { logger.info(TAG, `publishBasicNotification error, error = ${JSON.stringify(error)}`); } - } - + }; publishLongTextNotification = () => { try { logger.info(TAG, 'publishLongTextNotification'); - this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.long_text_notification')); - let notificationLongText = this.context.resourceManager.getStringSync($r('app.string.notification_long_text')); - let notificationBriefText = this.context.resourceManager.getStringSync($r('app.string.notification_brief_text')); - let notificationExpandedText = this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title')); - let notificationContent = notificationContentUtil.initNotificationLongTextContent(this.basicContent, notificationLongText, notificationBriefText, notificationExpandedText); + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.long_text_notification').id); + let notificationLongText = this.context.resourceManager.getStringSync($r('app.string.notification_long_text').id); + let notificationBriefText = + this.context.resourceManager.getStringSync($r('app.string.notification_brief_text').id); + let notificationExpandedText = + this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title').id); + let notificationContent = + notificationContentUtil.initNotificationLongTextContent(this.basicContent, notificationLongText, + notificationBriefText, notificationExpandedText); notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent as notificationManager.NotificationContent) as notificationManager.NotificationRequest); } catch (error) { logger.info(TAG, `publishLongTextNotification error, error = ${JSON.stringify(error)}`); } - } - + }; publishMultiLineNotification = () => { try { logger.info(TAG, 'publishMultiLineNotification'); - this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.multiline_notification')); - let notificationBriefText = this.context.resourceManager.getStringSync($r('app.string.notification_brief_text')); - let notificationLongTitle = this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title')); - let notificationContent = notificationContentUtil.initNotificationMultiLineContent(this.basicContent, notificationBriefText, notificationLongTitle, MULTI_LINE_CONTENT); + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.multiline_notification').id); + let notificationBriefText = + this.context.resourceManager.getStringSync($r('app.string.notification_brief_text').id); + let notificationLongTitle = + this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title').id); + let notificationContent = + notificationContentUtil.initNotificationMultiLineContent(this.basicContent, notificationBriefText, + notificationLongTitle, MULTI_LINE_CONTENT); notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent as notificationManager.NotificationContent) as notificationManager.NotificationRequest); } catch (error) { logger.info(TAG, `publishMultiLineNotification error, error = ${JSON.stringify(error)}`); } - } - + }; publishPictureNotification = async () => { try { logger.info(TAG, 'publishPictureNotification'); - this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.picture_notification')); - let notificationBriefText = this.context.resourceManager.getStringSync($r('app.string.notification_brief_text')); - let notificationExpandedText = this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title')); + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.picture_notification').id); + let notificationBriefText = + this.context.resourceManager.getStringSync($r('app.string.notification_brief_text').id); + let notificationExpandedText = + this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title').id); let imageArray = await this.context.resourceManager.getMediaContent($r('app.media.notification_icon').id); let imageResource = image.createImageSource(imageArray.buffer as ArrayBuffer); let picture = await imageResource.createPixelMap(); - let notificationContent = notificationContentUtil.initNotificationPictureContent(this.basicContent, notificationBriefText, notificationExpandedText, picture); + let notificationContent = + notificationContentUtil.initNotificationPictureContent(this.basicContent, notificationBriefText, + notificationExpandedText, picture); notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent as notificationManager.NotificationContent) as notificationManager.NotificationRequest); } catch (error) { logger.info(TAG, `publishPictureNotification error, error = ${JSON.stringify(error)}`); } - } - + }; publishConversationNotification = async () => { try { logger.info(TAG, 'publishConversationNotification'); - this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.conversation_notification')); + this.basicContent.title = + this.context.resourceManager.getStringSync($r('app.string.conversation_notification').id); let notificationContent = notificationContentUtil.initNotificationConversationContent(this.basicContent); - notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent as notificationManager.NotificationContent) as notificationManager.NotificationRequest, NOTIFICATION_GROUP.CONVERSATION); + notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent as notificationManager.NotificationContent) as notificationManager.NotificationRequest, + NOTIFICATION_GROUP.CONVERSATION); } catch (error) { 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')); + this.basicContent.title = + this.context.resourceManager.getStringSync($r('app.string.notification_with_buttons').id); let actionButtons: notificationManager.NotificationActionButton[] = [ { - title: this.context.resourceManager.getStringSync($r('app.string.first_button')), + title: this.context.resourceManager.getStringSync($r('app.string.first_button').id), wantAgent: await wantAgentUtil.createWantAgentForCommonEvent('') }, { - title: this.context.resourceManager.getStringSync($r('app.string.second_button')), + title: this.context.resourceManager.getStringSync($r('app.string.second_button').id), wantAgent: await wantAgentUtil.createWantAgentForStartAbility(BUNDLE_NAME, ABILITY_NAME) } ] let notificationContent = notificationContentUtil.initBasicNotificationContent(this.basicContent); - let notificationRequest = notificationRequestUtil.initButtonNotificationRequest(notificationContent, actionButtons); + 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')); + this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.basic_notification').id); let notificationWantAgent = await wantAgentUtil.createWantAgentForStartAbility(BUNDLE_NAME, ABILITY_NAME); let notificationContent = notificationContentUtil.initBasicNotificationContent(this.basicContent); - let notificationRequest = notificationRequestUtil.initWantAgentNotificationRequest(notificationContent, notificationWantAgent); + 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 -- Gitee