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 image from '@ohos.multimedia.image'; 17import notification from '@ohos.notificationManager'; 18import { logger, notificationUtil, notificationContentUtil, notificationRequestUtil } from '@ohos/notification'; 19 20const TAG: string = 'Sample_Notification'; 21const MULTI_LINE_CONTENT: Array<string> = ['line0', 'line1', 'line2', 'line3']; // 多行文本通知的多行文本内容 22 23interface NOTIFICATION_GROUP_TYPE { 24 BASIC: string 25 LONG_TEXT: string 26 MULTI_LINE: string 27 PICTURE: string 28 CONVERSATION: string 29} 30 31const NOTIFICATION_GROUP: NOTIFICATION_GROUP_TYPE = { // 定义不同类型通知的通知组 32 BASIC: 'BASIC', 33 LONG_TEXT: 'LONG_TEXT', 34 MULTI_LINE: 'MULTI_LINE', 35 PICTURE: 'PICTURE', 36 CONVERSATION: 'CONVERSATION' 37} 38 39export default class NotificationOperations { 40 private context: Context; 41 private basicContent: notification.NotificationBasicContent; 42 43 // 在初始化函数初始化基本通知类型的参数 44 constructor(context: Context) { 45 this.context = context; 46 let notificationTitle = ''; 47 let notificationText = this.context.resourceManager.getStringSync($r('app.string.notification_content')); 48 let notificationAdditional = this.context.resourceManager.getStringSync($r('app.string.notification_additional')); 49 this.basicContent = { 50 title: notificationTitle, 51 text: notificationText, 52 additionalText: notificationAdditional 53 } 54 } 55 56 // 发布基本类型通知 57 publishBasicNotification = () => { 58 try { 59 logger.info(TAG, 'publishBasicNotification'); 60 this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.basic_notification')); 61 let notificationContent = notificationContentUtil.initBasicNotificationContent(this.basicContent); 62 notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent)); 63 } catch (error) { 64 logger.info(TAG, `publishBasicNotification error, error = ${JSON.stringify(error)}`); 65 } 66 } 67 68 // 发布长文本类型通知 69 publishLongTextNotification = () => { 70 try { 71 logger.info(TAG, 'publishLongTextNotification'); 72 this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.long_text_notification')); 73 let notificationLongText = this.context.resourceManager.getStringSync($r('app.string.notification_long_text')); 74 let notificationBriefText = this.context.resourceManager.getStringSync($r('app.string.notification_brief_text')); 75 let notificationExpandedText = this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title')); 76 let notificationContent = notificationContentUtil.initNotificationLongTextContent(this.basicContent, notificationLongText, notificationBriefText, notificationExpandedText); 77 notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent)); 78 } catch (error) { 79 logger.info(TAG, `publishLongTextNotification error, error = ${JSON.stringify(error)}`); 80 } 81 } 82 83 // 发布多行文本类型通知 84 publishMultiLineNotification = () => { 85 try { 86 logger.info(TAG, 'publishMultiLineNotification'); 87 this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.multiline_notification')); 88 let notificationBriefText = this.context.resourceManager.getStringSync($r('app.string.notification_brief_text')); 89 let notificationLongTitle = this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title')); 90 let notificationContent = notificationContentUtil.initNotificationMultiLineContent(this.basicContent, notificationBriefText, notificationLongTitle, MULTI_LINE_CONTENT); 91 notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent)); 92 } catch (error) { 93 logger.info(TAG, `publishMultiLineNotification error, error = ${JSON.stringify(error)}`); 94 } 95 } 96 97 // 发布图片类型通知 98 publishPictureNotification = async () => { 99 try { 100 logger.info(TAG, 'publishPictureNotification'); 101 this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.picture_notification')); 102 let notificationBriefText = this.context.resourceManager.getStringSync($r('app.string.notification_brief_text')); 103 let notificationExpandedText = this.context.resourceManager.getStringSync($r('app.string.notification_expanded_title')); 104 let imageArray = await this.context.resourceManager.getMediaContent($r('app.media.notification_icon').id); 105 let imageResource = image.createImageSource(imageArray.buffer); 106 let picture = await imageResource.createPixelMap(); 107 let notificationContent = notificationContentUtil.initNotificationPictureContent(this.basicContent, notificationBriefText, notificationExpandedText, picture); 108 notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent)); 109 } catch (error) { 110 logger.info(TAG, `publishPictureNotification error, error = ${JSON.stringify(error)}`); 111 } 112 } 113 114 // 发布社交类型的通知 115 publishConversationNotification = async () => { 116 try { 117 logger.info(TAG, 'publishConversationNotification'); 118 this.basicContent.title = this.context.resourceManager.getStringSync($r('app.string.conversation_notification')); 119 let notificationContent = notificationContentUtil.initNotificationConversationContent(this.basicContent); 120 notificationUtil.publishNotification(notificationRequestUtil.initBasicNotificationRequest(notificationContent), NOTIFICATION_GROUP.CONVERSATION); 121 } catch (error) { 122 logger.info(TAG, `publishNotificationWithWantAgent error, error = ${JSON.stringify(error)}`); 123 } 124 } 125}