注意:发送消息通知要开启设置中的消息通知
import notify from '@ohos.notificationManager' import image from '@ohos.multimedia.image' import { BusinessError } from '@kit.BasicServicesKit' @Entry @Component struct NotificationPage { // 全局任务id idx: number = 100 // 图象 pixel: PixelMap | null = null async aboutToAppear() { // 获取资源管理器 let rm = getContext(this).resourceManager; // 读取图片 let file = await rm.getMediaContent($r('app.media.watchGT4')) // 创建PixelMap image.createImageSource(file.buffer).createPixelMap() .then(value => this.pixel = value) .catch((reason: BusinessError) => console.log('testTag', '加载图片异常', JSON.stringify(reason))) } build() { Column({ space: 20 }) { Button(`发送normalText通知`) .onClick(() => this.publishNormalTextNotification()) Button(`发送longText通知`) .onClick(() => this.publishLongTextNotification()) Button(`发送multiLine通知`) .onClick(() => this.publishMultiLineNotification()) Button(`发送Picture通知`) .onClick(() => this.publishPictureNotification()) } .width('100%') .height('100%') .padding(5) .backgroundColor('#f1f2f3') } publishNormalTextNotification() { let request: notify.NotificationRequest = { id: this.idx++, content: { notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: '通知标题' + this.idx, text: '通知内容详情', additionalText: '通知附加内容' } }, showDeliveryTime: true, deliveryTime: new Date().getTime(), groupName: 'wechat', notificationSlotType: notify.SlotType.SOCIAL_COMMUNICATION } this.publish(request) } publishLongTextNotification() { let request: notify.NotificationRequest = { id: this.idx++, content: { notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, longText: { title: '通知标题' + this.idx, text: '通知内容详情', additionalText: '通知附加内容', longText: '通知中的长文本,我很长,我很长,我很长,我很长,我很长,我很长,我很长', briefText: '通知概要和总结', expandedTitle: '通知展开时的标题' + this.idx } } } this.publish(request) } publishMultiLineNotification() { let request: notify.NotificationRequest = { id: this.idx++, content: { notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_MULTILINE, multiLine: { title: '通知标题' + this.idx, text: '通知内容详情', additionalText: '通知附加内容', briefText: '通知概要和总结', longTitle: '展开时的标题,我很宽,我很宽,我很宽', lines: [ '第一行', '第二行', '第三行', '第四行', ] } } } this.publish(request) } publishPictureNotification() { let request: notify.NotificationRequest = { id: this.idx++, content: { notificationContentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE, picture: { title: '通知标题' + this.idx, text: '通知内容详情', additionalText: '通知附加内容', briefText: '通知概要和总结', expandedTitle: '展开后标题' + this.idx, picture: this.pixel as PixelMap } } } this.publish(request) } private publish(request: notify.NotificationRequest) { console.log('notify test', '发送通知......') notify.publish(request) .then(() => console.log('notify test', '发送通知成功')) .catch((reason: BusinessError) => console.log('notify test', '发送通知失败', JSON.stringify(reason))) } }
标签:鸿蒙,idx,通知,request,publish,发送,notify From: https://www.cnblogs.com/longfeiPHP/p/18458543