首页 > 其他分享 >鸿蒙发送消息通知

鸿蒙发送消息通知

时间:2024-10-11 15:46:06浏览次数:1  
标签:鸿蒙 idx 通知 request publish 发送 notify

注意:发送消息通知要开启设置中的消息通知

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

相关文章

  • 【EI会议截稿通知】第九届计算机技术与机械电气工程国际学术论坛(ISCME 2024)
    第九届计算机技术与机械电气工程国际学术论坛(ISCME2024)20249th InternationalSeminaronComputerTechnology,MechanicalandElectricalEngineering重要信息大会官网:www.is-cme.com二轮截稿时间:2024年10月21日(早投稿,早录用)大会时间:2024年11月8-10日大会地点:中国......
  • 鸿蒙数据持久化sqlite
    1.数据查询model:entry/src/main/model/TaskModel.etsimportrelationalStorefrom'@ohos.data.relationalStore';import{common}from'@kit.AbilityKit';importTaskInfofrom'../ets/viewModel/TaskInfo';classTaskModel{priv......
  • 多行输入enter发送,enter+shif换行
    <textarea--><!--id="input-id"--><!--class="transparent-input1"--><!--:rows="2"--><!--placeholder="请输入内容,Shift+Enter换行"--><!--......
  • rabbitmq 发送端和消费端 dotnet
    #RabbitMQ发送端usingSystem.Text;usingRabbitMQ.Client;varfactory=newConnectionFactory();factory.HostName="127.0.0.1";//RabbitMQ服务器地址factory.DispatchConsumersAsync=true;stringexchangeName="exchange1";//交换机的名字string......
  • 鸿蒙初学001-构建第一个ArkTS应用(Stage模型)
    https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-dev-guide-V5HarmonyOSSDK介绍:从HarmonyOSNEXTDeveloperPreview1(API11)版本开始,HarmonyOSSDK以Kit维度提供丰富、完备的开放能力,涵盖应用框架、系统、媒体、图形、应用服务、AI六大领域,例如......
  • 鸿蒙首选项数据持久化
    1.ets/common/util/PreferencesUtils.etsimport{common}from'@kit.AbilityKit';import{preferences}from'@kit.ArkData';classPreferencesUtil{prefMap:Map<string,preferences.Preferences>=newMap()/*加载首选项*/lo......
  • tp6发送邮件功能如何实现?tp6的使用教程?
    tp6发送邮件有哪些技巧?tp6发送邮件步骤与设置指南?tp6(ThinkPHP6)作为一款流行的PHP框架,提供了强大的邮件发送功能,使得开发者能够轻松实现这一需求。AokSend将详细介绍如何在tp6中实现邮件发送功能。tp6发送邮件:模板管理在实际应用中,邮件内容通常需要根据不同的场景进行定制......
  • uibot发送邮件:自动化邮件发送教程详解!
    uibot发送邮件的操作指南?uibot发送邮件的两种方式?在现代办公环境中,自动化流程的引入极大地提高了工作效率。uibot发送邮件功能成为了许多企业和个人实现邮件自动化发送的首选工具。AokSend将详细介绍如何使用uibot发送邮件。uibot发送邮件:准备工作确保您已经安装并配置好了......
  • 鸿蒙NEXT开发-沉浸式导航和键盘避让模式(基于最新api12稳定版)
    注意:博主有个鸿蒙专栏,里面从上到下有关于鸿蒙next的教学文档,大家感兴趣可以学习下如果大家觉得博主文章写的好的话,可以点下关注,博主会一直更新鸿蒙next相关知识专栏地址:https://blog.csdn.net/qq_56760790/category_12794123.html目录1.沉浸式导航1.1基本介绍1.2如......
  • RabbitMQ目录发送方确认confirm:确认模式2.重复调用接口时,会提示错误所以改进,自己搞一
    目录发送方确认confirm:确认模式2.重复调用接口时,会提示错误所以改进,自己搞一个template,return退回模式:RabbitMQ的可靠性/保证消息不丢失..消息在交换机中无法路由到制定队列:return模式重试机制发送方确认当消息的生产者发送消息以后,怎么知道是否到达服务器呢?......