HarmonyOS鸿蒙—应用通知

紫色迷情 2024-09-11 ⋅ 6 阅读

简述

通知目的是为了让用户以合适的方式及时获取有用的新消息、帮助用户高效地处理任务

主要的使用场景

  • 显示接收到的短信息,即时信息等
  • 显示应用的推送消息,如广告、版本更新等
  • 显示当前正在进行的事件。如下载

消息的结构

  • 通知小图标
  • 通知名称
  • 时间
  • 展开箭头
  • 内容标题
  • 内容详情

通知种类

种类 基础类型通知 进度类型通知

基础类型通知

主要用于发送短信息,提示消息,广告推送等。开发的过程如下
导入模块 定义内容 发布内容

导入模块

import notification from '@ohos.notificationManager';

定义内容

let notificationRequest: notification.NotificationRequest = { // 描述通知的请求
      id: 1, // 通知ID
      slotType: notification.SlotType.SERVICE_INFORMATION,
      content: { // 通知内容
        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
        normal: { // 基本类型通知内容
          title: '通知内容标题',
          text: '通知内容详情',
          additionalText: '通知附加内容', // 通知附加内容,是对通知内容的补充。
        }
      }
    }
添加通知发布时间

使用下面两个属性:

 showDeliveryTime: true,
      deliveryTime: new Date().getTime(),

完整的使用方式如下:

let notificationRequest: notification.NotificationRequest = { // 描述通知的请求
      id: 1, // 通知ID
      
      showDeliveryTime: true,
      deliveryTime: new Date().getTime(),
      
      slotType: notification.SlotType.SERVICE_INFORMATION,
      content: { // 通知内容
        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
        normal: { // 基本类型通知内容
          title: '通知内容标题',
          text: '通知内容详情',
          additionalText: '通知附加内容', // 通知附加内容,是对通知内容的补充。
        }
      }
    }
设置通知图标
import image from '@ohos.multimedia.image';
async publishBasicText(){
let imageArray = await getContext(this).resouceManger.getMediaContent("");
let imageResource = image.creatImageSource();
let opts=[desiredSize:{height:,width:}]
let large =await imageResource.createPixelMap(this)
let nR: notification.NotificationRequest ={
	largerIcon: large,
	smallIcon:large
	}
}
设置操作按钮

属性部分的代码如下:

 actionButtons: [
    {
      title: '回复',
      wantAgent: wantAgentObj
    }
  ]

完整代码

let notificationRequest: notification.NotificationRequest = { // 描述通知的请求
      id: 1, // 通知ID
      slotType: notification.SlotType.SERVICE_INFORMATION,
      content: { // 通知内容
        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
        normal: { // 基本类型通知内容
          title: '通知内容标题',
          text: '通知内容详情',
          additionalText: '通知附加内容', // 通知附加内容,是对通知内容的补充。
        }
      },
       actionButtons: [
    {
      title: '回复',
      wantAgent: wantAgentObj
    }
 	 ]
    }
设置详情中大图
import image from '@ohos.multimedia.image';
// 将资源图片转化为PixelMap对象
    let resourceManager = getContext(this).resourceManager;
    let imageArray = await resourceManager.getMediaContent($r('app.media.bigPicture').id);
    let imageResource = image.createImageSource(imageArray.buffer);
    let pixelMap = await imageResource.createPixelMap();


 picture: {
          title: '通知内容标题',
          text: '通知内容详情',
          expandedTitle: '通知展开时的内容标题', // 通知展开时的内容标题
          briefText: '通知概要内容,是对通知内容的总结', // 通知概要内容,是对通知内容的总结
          picture: pixelMap // 通知的图片内容

发布通知

n.publish(nR).then(()=>{
}).catch((err)=>{
});

移除通知

notification.cancel(notificationId)
notification.cancelAll()

通知通道

用于设置通知的类型,只需要将类型slotType 设置为社交类型

slotType枚举值说明
SlotType.SOCIAL_COMMUNICATION社交类型,状态栏中显示通知图标,有横幅和提示音
SlotType.SERVICE_INFORMATION服务类型,状态栏中显示通知图标,没有横幅但有提示音
SlotType.CONTENT_INFORMATION内容类型,状态栏中显示通知图标,没有横幅或提示音
SlotType.OTHER_TYPES其它类型,状态栏中不显示通知图标,没有横幅或提示音

创建通知组

将不同类型的通知分为不同的组,以便用户可以更好的管理他们。当同组的通知有多条的时候,会自动折叠起来,避免通知比较多的时候,通知界面比较杂乱,例如当通知栏里有聊天消息通知和商品推荐通知时,我们只需要通过设置字段groupName,就可以对通知进行分组,给groupName设置不同的值可以将通知分为不同的组

let chatRequest: notification.NotificationRequest = { 
  id: notifyId++,
  groupName:'ChatGroup',
  content: {
    ...
   }
 };

进度类型通知

判断是否支持

发布进度类型时,要先查询系统是否支持。

notification.isSupportTemplate('downloadTemplate').then((data) => {
  console.info(`[ANS] isSupportTemplate success`);
  let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持
  // ...
}).catch((err) => {
  console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
});

模版

let template = {
  name: 'downloadTemplate',
  data: {
    progressValue: 60, // 当前进度值
    progressMaxValue: 100 // 最大进度值
   }
}

let notificationRequest = {
  id: 1,
  content: {
    contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    normal: {
      title: '文件下载:music.mp4',
      text: 'senTemplate',
      additionalText: '60%'
    }
  },
   template: template  
}
// 发布通知
notification.publish(notificationRequest).then(() => {
  console.info(`publish success`);
}).catch(error => {
  console.error(`[ANS] publish failed, code is ${error.code}, message is ${error.message}`);
})

全部评论: 0

    我有话说: