HarmonyOS 通知

一·通知简介:

  应用可以通过通知接口发送通知消息,终端用户可以通过通知栏查看通知内容,也可以点击通知来打开应用。

通知常见的使用场景:

  • 显示接收到的短消息、即时消息等。

  • 显示应用的推送消息,如广告、版本更新等。

  • 显示当前正在进行的事件,如下载等。

  基础类型通知主要应用于发送短信息、提示信息、广告推送等,支持普通文本类型、长文本类型、多行文本类型和图片类型,可以通过contentType指定通知的内容类型。

二·通知基础类型:

类型描述
NOTIFICATION_CONTENT_BASIC_TEXT普通文本类型
NOTIFICATION_CONTENT_LONG_TEXT长文本类型
NOTIFICATION_CONTENT_MULTILINE多行文本类型
NOTIFICATION_CONTENT_PICTURE图片类型

2.1普通文本通知

发布普通文本类型通知,需要设置contentType类型为ContentType.NOTIFICATION_CONTENT_BASIC_TEXT。

import notification from '@ohos.notificationManager';
 
@Entry
@Component
struct NotificationDemo {
  publishNotification() {
    let notificationRequest: notification.NotificationRequest = { // 描述通知的请求
      id: 1, // 通知ID
      slotType: notification.SlotType.SERVICE_INFORMATION,
      content: { // 通知内容
        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
        normal: { // 基本类型通知内容
          title: '通知内容标题',
          text: '通知内容详情',
          additionalText: '通知附加内容', // 通知附加内容,是对通知内容的补充。
        }
      }
    }
    notification.publish(notificationRequest).then(() => { // 发布通知
      console.info('publish success');
    }).catch((err) => {
      console.error(`publish failed, dcode:${err.code}, message:${err.message}`);
    });
  }
 
  build() {
    Column() {
      Button('发送通知')
        .onClick(() => {
          this.publishNotification()
        })
    }
    .width('100%')
  }
}

2.2长文本通知

发布长文本类型通知,需要设置contentType类型为ContentType.NOTIFICATION_CONTENT_LONG_TEXT。

let notificationRequest_long = {
  id:2,
  content:{
    contentType:NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,//长文本类型通知
    longText:{
      title:'长文本类型通知标题123',
      text:'测试内容567',
      additionalText:'长文本类型通知',
      longText: 'test_longText',
      briefText: 'test_briefText',
      expandedTitle: '长文本类型通知标题',
    }
  }
}

2.3多行文本通知

发布多行文本类型通知,需要设置contentType类型为ContentType.NOTIFICATION_CONTENT_MULTLINE。

let notificationRequest_multiline = {
  id:3,
  content:{
    contentType:NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,//多行文本类型通知
    multiLine:{
      title:'多行文本类型标题123',
      text:'多行文本类型内容567',
      briefText: 'test_briefText',
      longTitle: '多行文本类型',
      lines: ['1行', '2行', '3行', '4行','5行'],
 
    }
  }
}

2.4图片类型通知

发布图片类型通知,需要设置contentType类型为ContentType.NOTIFICATION_CONTENT_PICTURE。

 let notificationRequest: notification.NotificationRequest = { // 描述通知的请求
      id: 1,
      content: {
        contentType: notification.ContentType.NOTIFICATION_CONTENT_PICTURE,
        picture: {
          title: '好物热销中', // 通知内容标题
          text: '展开查看详情', // 通知内容
          expandedTitle: '今日热门推荐', // 通知展开时的内容标题
          briefText: '这里一定有您喜欢的', // 通知概要内容,是对通知内容的总结
          picture: pixelMap // 通知的图片内容
        }
      }
    }

三·发布进度条通知

进度条通知会展示一个动态的进度条,主要用于文件下载,长任务处理的进度显示

1.在发布进度类型通知前需要查询系统是否支持进度条模板。

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}]`);
});

2.构造进度条模板,name字段当前需要固定配置为downloadTemplate。

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

四、设置通知通道

通知通道类型:

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

  通过通知通道,您可让通知有不同的表现形式,比如社交类型的通知是横幅显示的,并且有提示音,而一般的通知则不会横幅显示,您可以使用slotType来实现,设置slotType为SlotType.SOCIAL_COMMUNICATION,表示为社交类型通知。

let imageArray = await getContext(this).resourceManager.getMediaContent($r('app.media.largeIcon').id);
let imageResource = image.createImageSource(imageArray.buffer);
let opts = { desiredSize: { height: 72, width: 72 } };
let largePixelMap = await imageResource.createPixelMap(opts);
let notificationRequest: notification.NotificationRequest = { // 描述通知的请求 
  id: 1, // 通知ID
  content: { // 通知内容
    contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
    slotType: notification.SlotType.SOCIAL_COMMUNICATION,
    normal: { // 基本类型通知内容
      title: '张三', // 通知内容标题。
      text: '等会下班一起吃饭哦', // 通知内容
    }
  },
  largeIcon: largePixelMap // 通知大图标。可选字段,大小不超过30KB。
}

五·给通知添加行为意图:

WantAgent提供了封装行为意图的能力,这里所说的行为意图主要是指拉起指定的应用组件及发布公共事件等能力。给通知添加行为意图后,点击通知后可以拉起指定的UIAbility或者发布公共事件。

5.1、导入模块。

import notification from '@ohos.notificationManager';
import wantAgent from '@ohos.app.ability.wantAgent';

5.2、创建WantAgentInfo信息。
场景一:拉起UIAbility。

var wantAgentInfo = {
  wants: [
    {
      bundleName: "com.example.notification",
      abilityName: "EntryAbility"
    }
  ],
  operationType: wantAgent.OperationType.START_ABILITY,
  requestCode: 100
}

场景二:发布公共事件。

let wantAgentInfo = {
  wants: [
    {
      action: 'event_name', // 设置事件名
      parameters: {},
    }
  ],
  operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
  requestCode: 100,
  wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
}
//创建WantAgent对象。
let wantAgentObj = null; 
wantAgent.getWantAgent(wantAgentInfo)
  .then((data) => {
    wantAgentObj = data;
  })
  .catch((err) => {
    console.error(`get wantAgent failed because ${JSON.stringify(err)}`);
  })


//构造NotificationRequest对象。
var notificationRequest = {
  id: 1,
  content: {
    contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    normal: {
      title: "通知标题",
      text: "通知内容"
    }
  },
  wantAgent: wantAgentObj
};


//发布WantAgent通知。
notification.publish(notificationRequest).then(() => { // 发布通知
  console.info("publish success");
}).catch((err) => {
  console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
});  

用户通过点击通知栏上的通知,即可触发WantAgent的动作。

  • 25
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值