W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
基礎類型通知主要應用于發(fā)送短信息、提示信息、廣告推送等,支持普通文本類型、長文本類型、多行文本類型和圖片類型。
表1 基礎類型通知中的內容分類
類型 | 描述 |
---|---|
NOTIFICATION_CONTENT_BASIC_TEXT | 普通文本類型。 |
NOTIFICATION_CONTENT_LONG_TEXT | 長文本類型。 |
NOTIFICATION_CONTENT_MULTILINE | 多行文本類型。 |
NOTIFICATION_CONTENT_PICTURE | 圖片類型。 |
目前系統(tǒng)僅通知欄訂閱了通知,將通知顯示在通知欄里?;A類型通知呈現(xiàn)效果示意圖如下所示。
根據(jù)設計樣式的不同,通知的實際顯示效果可能有所差異。本文中所涉及的通知效果圖僅供參考,請以實際運行結果為準。
圖1 基礎類型通知呈現(xiàn)效果示意圖
通知發(fā)布接口如下表所示,不同發(fā)布類型通知由NotificationRequest的字段攜帶不同的信息。
接口名 | 描述 |
---|---|
publish(request: NotificationRequest, callback: AsyncCallback<void>): void | 發(fā)布通知。 |
cancel(id: number, label: string, callback: AsyncCallback<void>): void | 取消指定的通知。 |
cancelAll(callback: AsyncCallback<void>): void; | 取消所有該應用發(fā)布的通知。 |
導入模塊。
- import NotificationManager from '@ohos.notificationManager';
構造NotificationRequest對象,并發(fā)布通知。
普通文本類型通知由標題、文本內容和附加信息三個字段組成,其中標題和文本內容是必填字段。
- let notificationRequest = {
- id: 1,
- content: {
- contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本類型通知
- normal: {
- title: 'test_title',
- text: 'test_text',
- additionalText: 'test_additionalText',
- }
- }
- }
- NotificationManager.publish(notificationRequest, (err) => {
- if (err) {
- console.error(`[ANS] failed to publish, error[${err}]`);
- return;
- }
- console.info(`[ANS] publish success`);
- });
運行效果如下圖所示。
長文本類型通知繼承了普通文本類型的字段,同時新增了長文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,內容為長文本內容。
- let notificationRequest = {
- id: 1,
- content: {
- contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 長文本類型通知
- longText: {
- title: 'test_title',
- text: 'test_text',
- additionalText: 'test_additionalText',
- longText: 'test_longText',
- briefText: 'test_briefText',
- expandedTitle: 'test_expandedTitle',
- }
- }
- }
- // 發(fā)布通知
- NotificationManager.publish(notificationRequest, (err) => {
- if (err) {
- console.error(`[ANS] failed to publish, error[${err}]`);
- return;
- }
- console.info(`[ANS] publish success`);
- });
運行效果如下圖所示。
多行文本類型通知繼承了普通文本類型的字段,同時新增了多行文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,多行文本內容多行顯示。
- let notificationRequest = {
- id: 1,
- content: {
- contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本類型通知
- multiLine: {
- title: 'test_title',
- text: 'test_text',
- briefText: 'test_briefText',
- longTitle: 'test_longTitle',
- lines: ['line_01', 'line_02', 'line_03', 'line_04'],
- }
- }
- }
- // 發(fā)布通知
- NotificationManager.publish(notificationRequest, (err) => {
- if (err) {
- console.error(`[ANS] failed to publish, error[${err}]`);
- return;
- }
- console.info(`[ANS] publish success`);
- });
運行效果如下圖所示。
圖片類型通知繼承了普通文本類型的字段,同時新增了圖片內容、內容概要和通知展開時的標題,圖片內容為PixelMap型對象,其大小不能超過2M。
- let imagePixelMap: PixelMap = undefined; // 需要獲取圖片PixelMap信息
- let notificationRequest: notificationManager.NotificationRequest = {
- id: 1,
- content: {
- contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
- picture: {
- title: 'test_title',
- text: 'test_text',
- additionalText: 'test_additionalText',
- briefText: 'test_briefText',
- expandedTitle: 'test_expandedTitle',
- picture: imagePixelMap
- }
- }
- };
- // 發(fā)布通知
- notificationManager.publish(notificationRequest, (err) => {
- if (err) {
- console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
- return;
- }
- console.info('Succeeded in publishing notification.');
- });
運行效果如下圖所示。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: