HarmonyOS APP开发之基本文本、长文本、多行文本、图片通知与自定义布局
HarmonyOS APP开发之基本文本、长文本、多行文本、图片通知与自定义布局
一、小知识
你有没有注意到,有些通知你一眼就会点开,而有些通知你连看都懒得看?区别往往在于内容的表现形式。
一条纯文本的"您有新消息"通知,和一条带图片、带预览、带操作按钮的富媒体通知,给用户的感受完全不同。前者像一张白纸条,后者像一张精美的明信片——内容一样,但体验天差地别。
HarmonyOS 提供了丰富的通知样式体系,通过 NotificationContent 的不同子类型,你可以根据内容特点选择最合适的展示形式。就像装修房子,不同的房间用不同的风格——客厅要大气、卧室要温馨、厨房要实用。通知样式也是一样,选对了样式,信息传递效率翻倍。
二、核心原理
2.1 通知样式体系总览
flowchart TD
A[NotificationContent] --> B[ContentType 枚举]
B --> C[NOTIFICATION_CONTENT_BASIC_TEXT<br/>基本文本]
B --> D[NOTIFICATION_CONTENT_LONG_TEXT<br/>长文本]
B --> E[NOTIFICATION_CONTENT_MULTI_LINE<br/>多行文本]
B --> F[NOTIFICATION_CONTENT_PICTURE<br/>图片通知]
C --> C1[NotificationContentText<br/>title + text]
D --> D1[NotificationLongTextContent<br/>title + text + longText]
E --> E1[NotificationMultiLineContent<br/>title + text + lines[]]
F --> F1[NotificationPictureContent<br/>title + text + picture]
classDef primary fill:#4CAF50,stroke:#388E3C,color:#fff
classDef warning fill:#FF9800,stroke:#F57C00,color:#fff
classDef error fill:#F44336,stroke:#D32F2F,color:#fff
classDef info fill:#2196F3,stroke:#1976D2,color:#fff
classDef purple fill:#9C27B0,stroke:#7B1FA2,color:#fff
class A,B primary
class C,D,E,F info
class C1,D1,E1,F1 warning
2.2 四种通知样式对比
| 样式 | ContentType | 内容对象 | 适用场景 | 展示特点 |
|---|---|---|---|---|
| 基本文本 | BASIC_TEXT | normal |
简短消息 | 标题+正文,最简洁 |
| 长文本 | LONG_TEXT | longText |
文章摘要、邮件正文 | 展开后显示完整长文本 |
| 多行文本 | MULTI_LINE | multiLine |
消息列表、待办事项 | 每行一条独立信息 |
| 图片通知 | PICTURE | picture |
图片分享、商品展示 | 附带一张图片 |
2.3 内容对象字段详解
// 基本文本内容
interface NotificationContentText {
title: string; // 通知标题(必填)
text: string; // 通知正文(必填)
}
// 长文本内容
interface NotificationLongTextContent {
title: string; // 通知标题(必填)
text: string; // 通知摘要(必填,折叠时显示)
longText: string; // 长文本正文(展开后显示)
}
// 多行文本内容
interface NotificationMultiLineContent {
title: string; // 通知标题(必填)
text: string; // 通知摘要(必填)
lines: Array<string>; // 多行文本数组(每行一条)
}
// 图片通知内容
interface NotificationPictureContent {
title: string; // 通知标题(必填)
text: string; // 通知正文(必填)
picture: image.PixelMap; // 图片对象(PixelMap格式)
}
2.4 通知样式选择流程
flowchart TD
A[需要发送通知] --> B{内容类型判断}
B -->|简短文本<br/>一句话能说清| C[基本文本样式]
B -->|长段落<br/>文章/邮件摘要| D[长文本样式]
B -->|多条信息<br/>消息列表/待办| E[多行文本样式]
B -->|有图片<br/>图片分享/商品| F[图片通知样式]
C --> G[ContentType.BASIC_TEXT]
D --> H[ContentType.LONG_TEXT]
E --> I[ContentType.MULTI_LINE]
F --> J[ContentType.PICTURE]
classDef primary fill:#4CAF50,stroke:#388E3C,color:#fff
classDef warning fill:#FF9800,stroke:#F57C00,color:#fff
classDef error fill:#F44336,stroke:#D32F2F,color:#fff
classDef info fill:#2196F3,stroke:#1976D2,color:#fff
classDef purple fill:#9C27B0,stroke:#7B1FA2,color:#fff
class A,B primary
class C,D,E,F info
class G,H,I,J warning
三、代码实战
3.1 基本文本与长文本通知
最常用的两种样式——基本文本适合"一句话通知",长文本适合"需要展开阅读"的场景。
import { notificationManager } from '@kit.NotificationKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct TextNotificationPage {
@State resultLog: string = '等待发布通知...';
/**
* 发布基本文本通知
* 最简单的通知形式,适合简短消息
*/
async publishBasicTextNotification(): Promise<void> {
const request: notificationManager.NotificationRequest = {
id: 5001,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: { // NotificationContentText 对象
title: '系统更新',
text: 'HarmonyOS 6.0 已发布,点击查看更新内容'
}
},
showDeliveryTime: true
};
try {
await notificationManager.publish(request);
this.resultLog = '✅ 基本文本通知已发布\n样式: BASIC_TEXT\n内容: 标题 + 正文';
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 发布失败: ${error.message}`;
}
}
/**
* 发布长文本通知
* 折叠时显示摘要,展开后显示完整长文本
* 适合邮件预览、文章摘要等场景
*/
async publishLongTextNotification(): Promise<void> {
const request: notificationManager.NotificationRequest = {
id: 5002,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: { // NotificationLongTextContent 对象
title: '新邮件',
text: '张三: 关于下周项目评审的安排', // 摘要(折叠时显示)
longText: '张三:\n\n关于下周项目评审的安排,我整理了以下几点需要确认:\n\n' +
'1. 评审时间:下周三下午2:00-4:00\n' +
'2. 评审地点:3楼会议室A\n' +
'3. 参会人员:项目组全体成员 + 产品经理\n' +
'4. 需要准备的材料:\n' +
' - 项目进度报告\n' +
' - 技术方案文档\n' +
' - 风险评估报告\n\n' +
'请各位提前准备好相关材料,如有疑问请及时沟通。\n\n' +
'此致\n张三'
}
},
showDeliveryTime: true
};
try {
await notificationManager.publish(request);
this.resultLog = '✅ 长文本通知已发布\n样式: LONG_TEXT\n折叠: 显示摘要\n展开: 显示完整内容';
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 发布失败: ${error.message}`;
}
}
build() {
Scroll() {
Column({ space: 16 }) {
Text('文本类通知样式')
.fontSize(24)
.fontWeight(FontWeight.Bold)
Text(this.resultLog)
.fontSize(14)
.width('90%')
.padding(16)
.borderRadius(12)
.backgroundColor('#F0F4F8')
.minHeight(80)
Button('发布基本文本通知')
.width('80%')
.height(50)
.backgroundColor('#4CAF50')
.onClick(() => this.publishBasicTextNotification())
Button('发布长文本通知')
.width('80%')
.height(50)
.backgroundColor('#2196F3')
.onClick(() => this.publishLongTextNotification())
Button('清除所有通知')
.width('80%')
.height(45)
.backgroundColor('#757575')
.onClick(async () => {
await notificationManager.cancelAll();
this.resultLog = '通知已清除';
})
}
.width('100%')
.padding(20)
}
.width('100%')
.height('100%')
}
}
3.2 多行文本通知
多行文本通知特别适合展示列表类信息——比如未读消息列表、待办事项汇总等。每行是一条独立的信息,用户一眼就能扫完。
import { notificationManager } from '@kit.NotificationKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct MultiLineNotificationPage {
@State resultLog: string = '等待发布多行通知...';
/**
* 发布多行文本通知 - 消息列表场景
* 每行显示一条消息的摘要
*/
async publishMessageListNotification(): Promise<void> {
const request: notificationManager.NotificationRequest = {
id: 5003,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTI_LINE,
multiLine: { // NotificationMultiLineContent 对象
title: '3条新消息',
text: '来自3个联系人的未读消息', // 摘要
lines: [ // 每行一条消息
'小明: 周末一起打球吗?',
'小红: 报告已经发到你邮箱了',
'工作群: 下午3点开会,请准时参加'
]
}
},
showDeliveryTime: true
};
try {
await notificationManager.publish(request);
this.resultLog = '✅ 消息列表通知已发布\n样式: MULTI_LINE\n行数: 3行';
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 发布失败: ${error.message}`;
}
}
/**
* 发布多行文本通知 - 待办事项场景
* 展示今日待办列表
*/
async publishTodoListNotification(): Promise<void> {
const request: notificationManager.NotificationRequest = {
id: 5004,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTI_LINE,
multiLine: {
title: '今日待办 (5项)',
text: '你有5项待办事项需要处理',
lines: [
'☐ 09:00 团队晨会',
'☐ 10:30 提交周报',
'☐ 14:00 代码评审',
'☐ 16:00 客户电话会议',
'☐ 18:00 健身房'
]
}
},
showDeliveryTime: true
};
try {
await notificationManager.publish(request);
this.resultLog = '✅ 待办列表通知已发布\n样式: MULTI_LINE\n行数: 5行';
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 发布失败: ${error.message}`;
}
}
/**
* 发布多行文本通知 - 物流追踪场景
* 展示物流节点信息
*/
async publishLogisticsNotification(): Promise<void> {
const request: notificationManager.NotificationRequest = {
id: 5005,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTI_LINE,
multiLine: {
title: '物流追踪',
text: '您的包裹正在配送中',
lines: [
'✅ 06-18 已发货 - 深圳仓库',
'✅ 06-19 运输中 - 广州中转站',
'✅ 06-20 派送中 - 您所在区域',
'⏳ 预计今日18:00前送达'
]
}
},
showDeliveryTime: true
};
try {
await notificationManager.publish(request);
this.resultLog = '✅ 物流追踪通知已发布\n样式: MULTI_LINE\n行数: 4行';
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 发布失败: ${error.message}`;
}
}
build() {
Scroll() {
Column({ space: 16 }) {
Text('多行文本通知')
.fontSize(24)
.fontWeight(FontWeight.Bold)
Text(this.resultLog)
.fontSize(14)
.width('90%')
.padding(16)
.borderRadius(12)
.backgroundColor('#F0F4F8')
.minHeight(80)
Button('消息列表通知')
.width('80%')
.height(50)
.backgroundColor('#2196F3')
.onClick(() => this.publishMessageListNotification())
Button('待办事项通知')
.width('80%')
.height(50)
.backgroundColor('#FF9800')
.onClick(() => this.publishTodoListNotification())
Button('物流追踪通知')
.width('80%')
.height(50)
.backgroundColor('#4CAF50')
.onClick(() => this.publishLogisticsNotification())
Button('清除所有通知')
.width('80%')
.height(45)
.backgroundColor('#757575')
.onClick(async () => {
await notificationManager.cancelAll();
this.resultLog = '通知已清除';
})
}
.width('100%')
.padding(20)
}
.width('100%')
.height('100%')
}
}
3.3 图片通知
图片通知是视觉冲击力最强的样式,适合图片分享、商品展示、照片通知等场景。需要注意图片必须转换为 PixelMap 格式。
import { notificationManager } from '@kit.NotificationKit';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit';
@Entry
@Component
struct PictureNotificationPage {
@State resultLog: string = '等待发布图片通知...';
/**
* 从资源文件创建 PixelMap
* 图片通知需要 PixelMap 格式的图片数据
*/
async createPixelMapFromResource(): Promise<image.PixelMap> {
try {
// 获取资源管理器
const resMgr = getContext(this).resourceManager;
// 读取图片资源(假设有一张通知图片放在 resources/rawfile/ 下)
const imageData = await resMgr.getRawFileContent('notification_image.jpg');
// 创建 ImageSource
const imageSource = image.createImageSource(imageData.buffer);
// 创建 PixelMap
const pixelMap = await imageSource.createPixelMap();
return pixelMap;
} catch (err) {
const error = err as BusinessError;
console.error(`[图片通知] 创建PixelMap失败: ${error.message}`);
throw error;
}
}
/**
* 发布图片通知
* 通知中附带一张图片,适合图片分享、商品展示等场景
*/
async publishPictureNotification(): Promise<void> {
try {
// 创建图片的 PixelMap
const pixelMap = await this.createPixelMapFromResource();
const request: notificationManager.NotificationRequest = {
id: 5006,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: { // NotificationPictureContent 对象
title: '图片分享',
text: '小明分享了一张照片给你',
picture: pixelMap // PixelMap 格式的图片
}
},
showDeliveryTime: true
};
await notificationManager.publish(request);
this.resultLog = '✅ 图片通知已发布\n样式: PICTURE\n图片: notification_image.jpg';
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 发布失败: ${error.message}`;
}
}
/**
* 使用纯色创建简单 PixelMap(演示用)
* 实际开发中应使用真实图片资源
*/
async createSimplePixelMap(): Promise<image.PixelMap> {
// 创建一个简单的纯色图片用于演示
const imageSource = image.createImageSource(
new Uint8Array([
0xFF, 0xD8, 0xFF, 0xE0, // JPEG 文件头(简化)
]).buffer
);
try {
const pixelMap = await imageSource.createPixelMap();
return pixelMap;
} catch (err) {
// 如果创建失败,返回一个空 PixelMap 的替代方案
console.warn('[图片通知] 简单PixelMap创建失败,使用资源文件');
return await this.createPixelMapFromResource();
}
}
build() {
Scroll() {
Column({ space: 16 }) {
Text('图片通知')
.fontSize(24)
.fontWeight(FontWeight.Bold)
Text(this.resultLog)
.fontSize(14)
.width('90%')
.padding(16)
.borderRadius(12)
.backgroundColor('#F0F4F8')
.minHeight(80)
Button('发布图片通知')
.width('80%')
.height(50)
.backgroundColor('#9C27B0')
.onClick(() => this.publishPictureNotification())
Button('清除所有通知')
.width('80%')
.height(45)
.backgroundColor('#757575')
.onClick(async () => {
await notificationManager.cancelAll();
this.resultLog = '通知已清除';
})
}
.width('100%')
.padding(20)
}
.width('100%')
.height('100%')
}
}
3.4 通知样式综合管理器
在实际项目中,你通常不会只发一种样式的通知。这个综合管理器封装了所有样式,提供统一的调用接口。
import { notificationManager } from '@kit.NotificationKit';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
/**
* 通知样式管理器
* 封装所有通知样式的发布逻辑,提供统一的调用接口
*/
class NotificationStyleManager {
private static instance: NotificationStyleManager;
private nextId: number = 6000;
// 单例模式
static getInstance(): NotificationStyleManager {
if (!NotificationStyleManager.instance) {
NotificationStyleManager.instance = new NotificationStyleManager();
}
return NotificationStyleManager.instance;
}
/**
* 生成唯一通知ID
*/
private generateId(): number {
return this.nextId++;
}
/**
* 发布基本文本通知
*/
async publishBasic(title: string, text: string): Promise<number> {
const id = this.generateId();
const request: notificationManager.NotificationRequest = {
id,
slotType: notificationManager.SlotType.SERVICE_INFORMATION,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: { title, text }
},
showDeliveryTime: true
};
await notificationManager.publish(request);
return id;
}
/**
* 发布长文本通知
*/
async publishLongText(title: string, summary: string, longText: string): Promise<number> {
const id = this.generateId();
const request: notificationManager.NotificationRequest = {
id,
slotType: notificationManager.SlotType.SERVICE_INFORMATION,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: {
title,
text: summary,
longText
}
},
showDeliveryTime: true
};
await notificationManager.publish(request);
return id;
}
/**
* 发布多行文本通知
*/
async publishMultiLine(title: string, summary: string, lines: string[]): Promise<number> {
const id = this.generateId();
const request: notificationManager.NotificationRequest = {
id,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTI_LINE,
multiLine: {
title,
text: summary,
lines
}
},
showDeliveryTime: true
};
await notificationManager.publish(request);
return id;
}
/**
* 发布图片通知
*/
async publishPicture(title: string, text: string, pixelMap: image.PixelMap): Promise<number> {
const id = this.generateId();
const request: notificationManager.NotificationRequest = {
id,
slotType: notificationManager.SlotType.CONTENT_INFORMATION,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: {
title,
text,
picture: pixelMap
}
},
showDeliveryTime: true
};
await notificationManager.publish(request);
return id;
}
/**
* 取消指定通知
*/
async cancel(id: number): Promise<void> {
await notificationManager.cancel(id);
}
/**
* 取消所有通知
*/
async cancelAll(): Promise<void> {
await notificationManager.cancelAll();
}
}
@Entry
@Component
struct NotificationStyleManagerPage {
@State resultLog: string = '通知样式管理器已就绪';
private styleManager: NotificationStyleManager = NotificationStyleManager.getInstance();
/**
* 演示基本文本通知
*/
async onBasicClick(): Promise<void> {
try {
const id = await this.styleManager.publishBasic(
'打卡提醒',
'上班时间到了,请记得打卡!'
);
this.resultLog = `✅ 基本文本通知已发布 (ID: ${id})`;
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 失败: ${error.message}`;
}
}
/**
* 演示长文本通知
*/
async onLongTextClick(): Promise<void> {
try {
const id = await this.styleManager.publishLongText(
'系统公告',
'HarmonyOS 6.0 正式发布',
'尊敬的开发者:\n\n' +
'HarmonyOS 6.0 已正式发布!本次更新包含以下重要变更:\n\n' +
'1. 全新的 ArkUI 声明式开发范式\n' +
'2. 增强的分布式能力\n' +
'3. 优化的通知管理系统\n' +
'4. 新增的 AI 辅助开发工具\n\n' +
'请尽快适配新版本,确保应用兼容性。\n\n' +
'——HarmonyOS 开发团队'
);
this.resultLog = `✅ 长文本通知已发布 (ID: ${id})`;
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 失败: ${error.message}`;
}
}
/**
* 演示多行文本通知
*/
async onMultiLineClick(): Promise<void> {
try {
const id = await this.styleManager.publishMultiLine(
'未读消息 (4条)',
'来自4个联系人的新消息',
[
'小明: 项目文档已更新',
'小红: 会议纪要已发送',
'工作群: 下周排班表出来了',
'系统: 你的密码即将过期'
]
);
this.resultLog = `✅ 多行文本通知已发布 (ID: ${id})`;
} catch (err) {
const error = err as BusinessError;
this.resultLog = `❌ 失败: ${error.message}`;
}
}
build() {
Scroll() {
Column({ space: 16 }) {
Text('通知样式管理器')
.fontSize(24)
.fontWeight(FontWeight.Bold)
Text(this.resultLog)
.fontSize(14)
.width('90%')
.padding(16)
.borderRadius(12)
.backgroundColor('#F0F4F8')
.minHeight(80)
Button('基本文本通知')
.width('80%')
.height(50)
.backgroundColor('#4CAF50')
.onClick(() => this.onBasicClick())
Button('长文本通知')
.width('80%')
.height(50)
.backgroundColor('#2196F3')
.onClick(() => this.onLongTextClick())
Button('多行文本通知')
.width('80%')
.height(50)
.backgroundColor('#FF9800')
.onClick(() => this.onMultiLineClick())
Button('清除所有通知')
.width('80%')
.height(45)
.backgroundColor('#757575')
.onClick(async () => {
await this.styleManager.cancelAll();
this.resultLog = '所有通知已清除';
})
}
.width('100%')
.padding(20)
}
.width('100%')
.height('100%')
}
}
四、踩坑与注意事项
4.1 ContentType 与内容对象必须匹配
这是最常见的错误!contentType 和实际的内容对象必须严格对应,否则会抛出异常。
// ❌ 错误示范:contentType 和内容对象不匹配
const request = {
id: 1,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
longText: { title: '标题', text: '摘要', longText: '长文本' } // 错误!
// contentType 是 BASIC_TEXT,但内容对象用了 longText
}
};
// ✅ 正确示范:两者必须匹配
const request = {
id: 1,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: { title: '标题', text: '摘要', longText: '长文本' }
}
};
4.2 长文本的长度限制
longText 字段虽然没有严格的字符数限制,但通知中心展示区域有限。建议控制在 500 字以内,过长的内容用户不会阅读。
4.3 多行文本的行数建议
lines 数组建议不超过 5 行。超过 5 行后,通知中心可能只显示前几行,其余行需要展开才能看到,降低了信息传达效率。
4.4 图片通知的图片尺寸
图片通知中的图片不宜过大:
- 推荐尺寸:宽度不超过 400px,高度不超过 200px
- 格式要求:必须为
PixelMap格式,不支持 URL 直接引用 - 内存注意:大图片会占用较多内存,建议在发布后及时释放 PixelMap 资源
// ✅ 发布图片通知后释放 PixelMap
await notificationManager.publish(request);
pixelMap.release(); // 释放图片资源,避免内存泄漏
4.5 通知内容的本地化
如果你的应用支持多语言,通知内容也需要做国际化处理:
// ❌ 硬编码中文
title: '新消息'
// ✅ 使用国际化资源
title: $r('app.string.notification_new_message')
4.6 通知样式与渠道的配合
不同渠道适合不同的通知样式。高优先级的社交通信渠道适合多行文本(消息列表),低优先级的内容渠道适合基本文本(新闻标题)。选择样式时要考虑渠道的展示特点。
五、HarmonyOS 6 适配
5.1 API 变更
| 变更项 | HarmonyOS 5 | HarmonyOS 6 |
|---|---|---|
| 内容类型 | 4种基本类型 | 新增 CONVERSATION_TYPE(对话通知) |
| 图片支持 | PixelMap | 新增支持 URI 引用 |
| 长文本限制 | 无明确限制 | 建议不超过 500 字 |
| 自定义布局 | 不支持 | 新增 LiveView 实时通知 |
5.2 迁移指南
- 对话通知:HarmonyOS 6 新增了对话类型通知,适合即时通讯场景:
// HarmonyOS 6 对话通知(新特性)
const request = {
id: 1,
content: {
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_CONVERSATION,
conversation: {
title: '与张三的对话',
text: '最新的消息内容',
// 对话通知特有字段
messages: [...],
person: {...}
}
}
};
-
LiveView 实时通知:适用于进度条、计时器等需要实时更新的场景,替代了之前通过频繁更新通知来实现的方式。
-
图片 URI 支持:HarmonyOS 6 允许直接使用图片 URI 而非 PixelMap,简化了图片通知的发布流程。
六、总结
mindmap
root((通知样式))
四种基本样式
基本文本 BASIC_TEXT
title + text
最简洁的通知
长文本 LONG_TEXT
title + text + longText
折叠/展开双模式
多行文本 MULTI_LINE
title + text + lines[]
列表类信息展示
图片通知 PICTURE
title + text + picture
视觉冲击力最强
核心对象
NotificationContentText
NotificationLongTextContent
NotificationMultiLineContent
NotificationPictureContent
选择原则
一句话 → 基本文本
长段落 → 长文本
多条信息 → 多行文本
有图片 → 图片通知
注意事项
ContentType与内容对象必须匹配
长文本建议500字以内
多行建议不超过5行
图片需PixelMap格式
图片资源及时释放
内容需要国际化
| 知识点 | 要点 |
|---|---|
| 基本文本 | normal: { title, text },最简洁的通知形式 |
| 长文本 | longText: { title, text, longText },折叠时显示摘要,展开显示全文 |
| 多行文本 | multiLine: { title, text, lines[] },适合消息列表和待办事项 |
| 图片通知 | picture: { title, text, picture },图片必须为 PixelMap 格式 |
| ContentType 匹配 | contentType 枚举值必须与内容对象字段严格对应 |
| 图片资源释放 | 发布图片通知后调用 pixelMap.release() 避免内存泄漏 |
| 样式选择 | 根据内容特点选择最合适的样式,不要"大材小用" |
通知样式决定了信息的呈现方式,选对样式能让通知的传达效率翻倍。下一篇我们将进入提醒通知的世界,看看如何让通知在特定时间自动"敲门"。
- 点赞
- 收藏
- 关注作者
评论(0)