鴻蒙傳送訊息通知

龍飛鳯舞發表於2024-10-11

注意:傳送訊息通知要開啟設定中的訊息通知

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)))
  }
}

相關文章