發(fā)布基礎類型通知

2024-02-16 13:50 更新

基礎類型通知主要應用于發(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ā)布的通知。

開發(fā)步驟

  1. 導入模塊。

    1. import NotificationManager from '@ohos.notificationManager';
  2. 構造NotificationRequest對象,并發(fā)布通知。

    • 普通文本類型通知由標題、文本內容和附加信息三個字段組成,其中標題和文本內容是必填字段。

      1. let notificationRequest = {
      2. id: 1,
      3. content: {
      4. contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本類型通知
      5. normal: {
      6. title: 'test_title',
      7. text: 'test_text',
      8. additionalText: 'test_additionalText',
      9. }
      10. }
      11. }
      12. NotificationManager.publish(notificationRequest, (err) => {
      13. if (err) {
      14. console.error(`[ANS] failed to publish, error[${err}]`);
      15. return;
      16. }
      17. console.info(`[ANS] publish success`);
      18. });

      運行效果如下圖所示。

    • 長文本類型通知繼承了普通文本類型的字段,同時新增了長文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,內容為長文本內容。

      1. let notificationRequest = {
      2. id: 1,
      3. content: {
      4. contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 長文本類型通知
      5. longText: {
      6. title: 'test_title',
      7. text: 'test_text',
      8. additionalText: 'test_additionalText',
      9. longText: 'test_longText',
      10. briefText: 'test_briefText',
      11. expandedTitle: 'test_expandedTitle',
      12. }
      13. }
      14. }
      15. // 發(fā)布通知
      16. NotificationManager.publish(notificationRequest, (err) => {
      17. if (err) {
      18. console.error(`[ANS] failed to publish, error[${err}]`);
      19. return;
      20. }
      21. console.info(`[ANS] publish success`);
      22. });

      運行效果如下圖所示。

    • 多行文本類型通知繼承了普通文本類型的字段,同時新增了多行文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,多行文本內容多行顯示。

      1. let notificationRequest = {
      2. id: 1,
      3. content: {
      4. contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本類型通知
      5. multiLine: {
      6. title: 'test_title',
      7. text: 'test_text',
      8. briefText: 'test_briefText',
      9. longTitle: 'test_longTitle',
      10. lines: ['line_01', 'line_02', 'line_03', 'line_04'],
      11. }
      12. }
      13. }
      14. // 發(fā)布通知
      15. NotificationManager.publish(notificationRequest, (err) => {
      16. if (err) {
      17. console.error(`[ANS] failed to publish, error[${err}]`);
      18. return;
      19. }
      20. console.info(`[ANS] publish success`);
      21. });

      運行效果如下圖所示。

    • 圖片類型通知繼承了普通文本類型的字段,同時新增了圖片內容、內容概要和通知展開時的標題,圖片內容為PixelMap型對象,其大小不能超過2M。

      1. let imagePixelMap: PixelMap = undefined; // 需要獲取圖片PixelMap信息
      2. let notificationRequest: notificationManager.NotificationRequest = {
      3. id: 1,
      4. content: {
      5. contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
      6. picture: {
      7. title: 'test_title',
      8. text: 'test_text',
      9. additionalText: 'test_additionalText',
      10. briefText: 'test_briefText',
      11. expandedTitle: 'test_expandedTitle',
      12. picture: imagePixelMap
      13. }
      14. }
      15. };
      16. // 發(fā)布通知
      17. notificationManager.publish(notificationRequest, (err) => {
      18. if (err) {
      19. console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
      20. return;
      21. }
      22. console.info('Succeeded in publishing notification.');
      23. });

      運行效果如下圖所示。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號