通知是手机系统中很重要的信息展示方式,通知不仅可以展示文字,也可以展示图片,甚至可以将组件加到通知中,只要用户不清空,通知的信息可以永久保留在状态栏上
通知的介绍
通知 Notification
通知,即在一个应用的UI界面之外显示的消息,主要是用来提醒用户有来自该应用中的消息。
当应用向系统发出通知时,它将先以图标的形式显示在通知栏中,用户可以下拉通知栏查看详细信息。
常见的使用场景:
1、显示推送的短消息、即时消息等
2、显示应用的推送消息,如广告、版本信息等
3、显示当前正在进行的事件,如播放音乐、导航、下载等
通知的使用
1、需要先定义需要发送通知的NotificationRequest
// 描述通知的请求 let notificationRequest: notificationManager.NotificationRequest = {//id为通知的唯一标识,用于通讯的通知与取消//如果没有指定id,或者id相同,后发送的通知会将先发送的通知覆盖掉id: 100,content: {//notificationContentType定义通知的类型,如普通文本、长文本等notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,//normal定义普通文本类型通知的内容normal: {//title定义通知内容标题title: `通知内容标题`,//title定义通知内容详情text: '通知内容详情',//附加文本additionalText:"通知附加文本"}},//角标,不点击的情况下会叠加的,每次叠加“1”badgeNumber:1, }
2、调用notificationManager.publish(notificationRequest)方法发布通知
// 发送通知 notificationManager.publish(notificationRequest).then(()=>{console.info('publish success') }).catch((err: Error) => {console.error(`publish failed,message is ${err}`); });
基本文本通知
//基础文本通知
import { notificationManager } from '@kit.NotificationKit';
// 描述通知的请求
let notificationRequest: notificationManager.NotificationRequest = {//id为通知的唯一标识,用于通讯的通知与取消id: 100,content: {//notificationContentType定义通知的类型notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,//normal定义普通文本类型通知的内容normal: {//title定义通知内容标题title: `通知内容标题`,//title定义通知内容详情text: '通知内容详情'}}
}
// 发送通知
notificationManager.publish(notificationRequest).then(()=>{console.info('publish success')
}).catch((err: Error) => {console.error(`publish failed,message is ${err}`);
});