自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS自定義常用通知欄

開發(fā) 前端 OpenHarmony
通知(Notification)旨在讓用戶以合適的方式及時獲得有用的新消息,幫助用戶高效地處理任務(wù)。系統(tǒng)為開發(fā)者提供了不同種類的通知樣式模板可以使用,開發(fā)者也可以根據(jù)自己需要自定義通知樣式。

[[419221]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

通知(Notification)旨在讓用戶以合適的方式及時獲得有用的新消息,幫助用戶高效地處理任務(wù)。

系統(tǒng)為開發(fā)者提供了不同種類的通知樣式模板可以使用,開發(fā)者也可以根據(jù)自己需要自定義通知樣式。

HarmonyOS通知相關(guān)類

與通知相關(guān)的主要邏輯在NotificationSlotNotificationRequestNotificationHelper這三個類中,那這三個類都有什么作用呢?下面為大家逐一介紹。

1. NotificationSlot

這是一個定義通知的主題類,它可以設(shè)置通知的特征集合,包括通知到來時的提示音調(diào)、振動、鎖屏顯示以及設(shè)置通知的重要級別等。一般可以在應(yīng)用的AbilityPackage里設(shè)置,用以統(tǒng)一整個應(yīng)用的通知主題特征,一個應(yīng)用也可以關(guān)聯(lián)多個不同NotificationSlot。

重點說下NotificationSlot的幾個重要級別,也可以查看官方Api文檔:

  • LEVEL_NONE: 表示通知不發(fā)布。
  • LEVEL_MIN:表示通知可以發(fā)布,但是不顯示在通知欄,不自動彈出,無提示音;該級別不適用于前臺服務(wù)的場景。
  • LEVEL_LOW:表示通知可以發(fā)布且顯示在通知欄,不自動彈出,無提示音。
  • LEVEL_DEFAULT:表示通知發(fā)布后可在通知欄顯示,不自動彈出,觸發(fā)提示音。
  • LEVEL_HIGH:表示通知發(fā)布后可在通知欄顯示,自動彈出,觸發(fā)提示音。

代碼示例

  1. // 創(chuàng)建notificationSlot對象 
  2. NotificationSlot slot = new NotificationSlot(id, "testSlot", NotificationSlot.LEVEL_HIGH); 
  3. slot.setDescription("create notificationSlot description"); 
  4. slot.setLevel(NotificationSlot.LEVEL_HIGH); 
  5. // 設(shè)置振動提醒 
  6. slot.setEnableVibration(true); 
  7. // 設(shè)置鎖屏模式 
  8. slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC); 
  9. // 設(shè)置開啟呼吸燈提醒 
  10. slot.setEnableLight(true); 

  11. // 設(shè)置呼吸燈的提醒顏色 
  12. slot.setLedLightColor(Color.RED.getValue()); 
  13. slot.enableBypassDnd(true); 
  14. slot.enableBadge(true); 
  15. try { 
  16.     NotificationHelper.addNotificationSlot(slot); 
  17. } catch (RemoteException e) { 
  18.     e.printStackTrace(); 

關(guān)于設(shè)置呼吸燈說明,由于手上只有一部P40Pro不帶呼吸燈,所以無法驗證實際效果。

2. NotificationRequest

NotificationRequest是通知最主要的部分,主要設(shè)置通知的樣式,HarmonyOS主要提供了6種類型的樣式:普通文本NotificationNormalContent、長文本NotificationLongTextContent、圖片NotificationPictureContent、多行NotificationMultiLineContent、社交NotificationConversationalContent、媒體NotificationMediaContent。另外還有一種自定義樣式,這些會在后面具體介紹。

雖然通知中提供了各種屬性的設(shè)置,但是一個通知對象,有幾個屬性是必須要設(shè)置的,其他的屬性均是可選的,必須設(shè)置的屬性如下:

  • 小圖標(biāo),使用setLittleIcon()方法設(shè)置。
  • 標(biāo)題,使用setTitle()方法設(shè)置。
  • 文本內(nèi)容,使用setText()方法設(shè)置。

調(diào)用setIntentAgent()設(shè)置通知可以觸發(fā)的事件

  1. Intent intent = new Intent(); 
  2. // 指定要啟動的Ability的BundleName和AbilityName字段 
  3. // 將Operation對象設(shè)置到Intent中 
  4. Operation operation = new Intent.OperationBuilder() 
  5.         .withDeviceId(""
  6.         .withBundleName(getBundleName()) 
  7.         .withAbilityName(OtherAbility.class.getName()) 
  8.         .build(); 
  9. intent.setOperation(operation); 
  10. List<Intent> intentList = new ArrayList<>(); 
  11. intentList.add(intent); 
  12. // 定義請求碼 
  13. int requestCode = 200; 
  14. // 設(shè)置flags 
  15. List<IntentAgentConstant.Flags> flags = new ArrayList<>(); 
  16. flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG); 
  17. // 指定啟動一個有頁面的Ability 
  18. IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,  
  19.         IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null); 
  20. // 獲取IntentAgent實例 
  21. IntentAgent agent = IntentAgentHelper.getIntentAgent(this, paramsInfo); 
  22. setIntentAgent(agent); 

具體API就不一一介紹了,可以參考官方

3. NotificationHelper

該靜態(tài)類主要是管理通知,提供了發(fā)布、更新、刪除通知等靜態(tài)方法;

主要接口如下:

  • publishNotification(NotificationRequest request),發(fā)布通知,當(dāng)NotificationRequest被設(shè)置后,通過該接口去發(fā)布通知;
  • cancelNotification(int notificationId),取消通知,每個NotificationRequest創(chuàng)建時都必須有一個notificationId,可以通過這個接口取消創(chuàng)建的通知;
  • cancelAllNotifications(),取消之前發(fā)布的所有通知;
  • addNotificationSlot(NotificationSlot slot),創(chuàng)建一個NotificationSlot;
  • setNotificationBadgeNum(int num),設(shè)置通知的角標(biāo);

通知的代碼結(jié)構(gòu)基本就是圍繞這三個類來構(gòu)建的,其中最重要的就是NotificationRequest這個類,整個HarmonyOS各種酷炫通知都是基于這個類來定制的,所以研究通知,不如說其實就是研究NotificationRequest。下面就來介紹下HarmonyOS官方提供的6中樣式以及自定義樣式,基本也就包含日常所有的通知需求了。

HarmonyOS通知樣式

1. 普通文本NotificationNormalContent

這是通知最基礎(chǔ)也是最常用的樣式,對應(yīng)設(shè)置NotificationRequest.setLittleIcon()、NotificationNormalContent.setTitle()、NotificationNormalContent.setText();

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. int notificationId = 1; 
  2. NotificationRequest request = new NotificationRequest(notificationId); 
  3. request.setSlotId(slotId); 
  4. request.setLittleIcon(littleIcon); 
  5.  
  6. // 普通文本 
  7. NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent(); 
  8. content.setTitle(title) 
  9.        .setText(countent); 
  10. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content); 
  11. // 設(shè)置通知的內(nèi)容 
  12. request.setContent(notificationContent); 
  13. request.setIntentAgent(intentAgent); 
  14. try { 
  15.     NotificationHelper.publishNotification(request); 
  16. } catch (RemoteException e) { 
  17.     e.printStackTrace(); 
  1. /** 
  2.  * 圖片轉(zhuǎn)換工具方法 
  3.  * 
  4.  * @param drawableId 
  5.  * @return 
  6.  */ 
  7. private PixelMap getPixelMap(int drawableId) { 
  8.     InputStream drawableInputStream = null
  9.     try { 
  10.         drawableInputStream = context.getResourceManager().getResource(drawableId); 
  11.         ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); 
  12.         ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions); 
  13.         ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); 
  14.         decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888; 
  15.         return imageSource.createPixelmap(decodingOptions); 
  16.     } catch (IOException | NotExistException e) { 
  17.         e.getMessage(); 
  18.     } finally { 
  19.         if (drawableInputStream != null) { 
  20.         try { 
  21.             drawableInputStream.close(); 
  22.         } catch (IOException e) { 
  23.             e.getMessage(); 
  24.         } 
  25.     } 
  26. return null

2. 長文本NotificationLongTextContent

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)
【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. int notificationId = 2; 
  2. NotificationRequest request = new NotificationRequest(notificationId); 
  3. request.setSlotId(slotId); 
  4. request.setLittleIcon(littleIcon); 
  5. // request.setBigIcon(bigIcon); 
  6.  
  7. // 長文本 
  8. NotificationRequest.NotificationLongTextContent contentLong = new NotificationRequest.NotificationLongTextContent(); 
  9. contentLong.setTitle(title) 
  10.            .setLongText(longText); 
  11. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(contentLong); 
  12. // 設(shè)置通知的內(nèi)容 
  13. request.setContent(notificationContent); 
  14. request.setIntentAgent(intentAgent); 
  15. try { 
  16.     NotificationHelper.publishNotification(request); 
  17. } catch (RemoteException e) { 
  18.     e.printStackTrace(); 

3. 圖片NotificationPictureContent

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)
【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. int notificationId = 4; 
  2. NotificationRequest request = new NotificationRequest(notificationId); 
  3. request.setSlotId(slotId); 
  4. request.setLittleIcon(littleIcon); 
  5. request.setBigIcon(icon); 
  6.  
  7. // 圖片通知 
  8. NotificationRequest.NotificationPictureContent contentLong = new NotificationRequest.NotificationPictureContent(); 
  9. contentLong.setTitle(title) 
  10.            .setBigPicture(icon) 
  11.            .setExpandedTitle(title) 
  12.            .setText(context); 
  13. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(contentLong); 
  14. // 設(shè)置通知的內(nèi)容 
  15. request.setContent(notificationContent); 
  16. request.setIntentAgent(intentAgent); 
  17. try { 
  18.     NotificationHelper.publishNotification(request); 
  19. } catch (RemoteException e) { 
  20.     e.printStackTrace(); 

4. 多行NotificationMultiLineContent

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)
【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. int notificationId = 5; 
  2. NotificationRequest request = new NotificationRequest(notificationId); 
  3. request.setSlotId(slot.getId()); 
  4. request.setLittleIcon(littleIcon); 
  5.  
  6. // 多行文本 
  7. NotificationRequest.NotificationMultiLineContent multiLineContent = new NotificationRequest.NotificationMultiLineContent(); 
  8. multiLineContent.setTitle("工資單"
  9.                 .setText("保密文件,禁止傳遞"
  10.                 .addSingleLine("基礎(chǔ)工資: 210000"
  11.                 .addSingleLine("加班補助: 97630"
  12.                 .addSingleLine("餐補: 900"
  13.                 .addSingleLine("交通補助: 1200"
  14.                 .addSingleLine("出差補助: 9800"
  15.                 .setExpandedTitle("張學(xué)友工資單"); 
  16. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(multiLineContent); 
  17. // 設(shè)置通知的內(nèi)容 
  18. request.setContent(notificationContent); 
  19. request.setIntentAgent(intentAgent); 
  20. try { 
  21.     NotificationHelper.publishNotification(request); 
  22. } catch (RemoteException e) { 
  23.     e.printStackTrace(); 

5. 社交NotificationConversationalContent

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)
【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. ArrayList<String> arrayListStr = new ArrayList<>(); 
  2. arrayListStr.add("結(jié)婚以后兩個人在一起最重要的是什么?"); 
  3. arrayListStr.add("你是如何走出人生的陰霾的?"); 
  4. arrayListStr.add("怎么不回復(fù)我??我生氣了??!"); 
  5. arrayListStr.add("我真生氣了!?。。?!你聽見了嗎!"); 
  6. arrayListStr.add("為什么新聞放完了總是要播出他們在收拾稿子的片段?"); 
  7.  
  8. MessageUser messageUser = new MessageUser(); 
  9. messageUser.setName(name); 
  10. messageUser.setPixelMap(icon); 
  11.  
  12. int notificationId = 3; 
  13. NotificationRequest request = new NotificationRequest(notificationId); 
  14. request.setSlotId(slot.getId()); 
  15. request.setLittleIcon(littleIcon); 
  16. request.addMessageUser(messageUser); 
  17.  
  18. // 社交 
  19. NotificationRequest.NotificationConversationalContent content = new NotificationRequest.NotificationConversationalContent(messageUser); 
  20. content.setConversationTitle("[" + arrayListStr.size() + "條]" + name
  21.        .setConversationGroup(true); 
  22. for (int i = 0; i < arrayListStr.size(); i++) { 
  23.     content.addConversationalMessage(arrayListStr.get(i), 1, messageUser); 
  24.  
  25. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content); 
  26. // 設(shè)置通知的內(nèi)容 
  27. request.setContent(notificationContent); 
  28. request.setIntentAgent(intentAgent); 
  29. try { 
  30.     NotificationHelper.publishNotification(request); 
  31. } catch (RemoteException e) { 
  32.     e.printStackTrace(); 

6. 媒體NotificationMediaContent

具體媒體會話管理,請參考開發(fā)-媒體會話管理開發(fā)指導(dǎo)

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. // 按鈕文字設(shè)置無效,圖標(biāo)顏色也不生效,默認(rèn)都是灰色 
  2. NotificationActionButton.Builder builder = new NotificationActionButton.Builder(pixelMap1, "btn1"null); 
  3. NotificationActionButton.Builder builder1 = new NotificationActionButton.Builder(pixelMap2, "btn2"null); 
  4. NotificationActionButton.Builder builder2 = new NotificationActionButton.Builder(pixelMap3, "btn3"null); 
  5.  
  6. int notificationId = 1; 
  7. NotificationRequest request = new NotificationRequest(notificationId); 
  8. request.setSlotId(slot.getId()); 
  9. request.setLittleIcon(littleIcon); 
  10. request.addActionButton(builder.build()); 
  11. request.addActionButton(builder1.build()); 
  12. request.addActionButton(builder2.build()); 
  13.  
  14. int[] a = {0, 1, 2}; 
  15. // 普通文本 
  16. // setAVToken 將指定的AVToken附加,連接AVToken后,此通知可以與關(guān)聯(lián)的AVSession交互,以便用戶可以在此通知中控制媒體播放 
  17. NotificationRequest.NotificationMediaContent mediaContent = new NotificationRequest.NotificationMediaContent(); 
  18. mediaContent.setTitle(title) 
  19.             .setText(conStr) 
  20.             .setAVToken(avBrowser.getAVToken()) 
  21.             .setShownActions(a); 
  22. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(mediaContent); 
  23. // 設(shè)置通知的內(nèi)容 
  24. request.setContent(notificationContent); 
  25. try { 
  26.     NotificationHelper.publishNotification(request); 
  27. } catch (RemoteException e) { 
  28.     e.printStackTrace(); 

7. 自定義通知樣式

效果圖

【中軟國際】HarmonyOS自定義常用通知欄-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼示例

  1. NotificationRequest request = new NotificationRequest(context, 5); 
  2. request.setSlotId(slot.getId()); 
  3. request.setLittleIcon(littleIcon); 
  4.  
  5. String title = ""
  6. String text = ""
  7. NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent(); 
  8. content.setTitle(title).setText(text); 
  9. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content); 
  10. request.setContent(notificationContent); 
  11.  
  12. // layoutId就是自己定義的xml布局,需要在xml的父布局中設(shè)置一個卡片屬性“ohos:remote="true"”,否則自定義效果無法出現(xiàn) 
  13. ComponentProvider componentProvider = new ComponentProvider(layoutId, context); // 創(chuàng)建ComponentProvider對象 
  14. // componentProvider.setString(ResourceTable.Id_ongoing_card_text, "setText""TextContent"); // 設(shè)置布局中的文本內(nèi)容 
  15. request.setCustomView(componentProvider); 
  16. request.setIntentAgent(intentAgent); 
  17.  
  18. try { 
  19.     NotificationHelper.publishNotification(request); 
  20. } catch (RemoteException e) { 
  21.     e.printStackTrace(); 

上面這些就是通知常用的幾種效果,有很多其他的屬性沒有在demo中展示出來,比如角標(biāo)、通知欄進度條等,這些都有屬性可以設(shè)置的,相比其他移動操作系統(tǒng),鴻蒙的通知樣式更加豐富全面也更加統(tǒng)一,相對來說開發(fā)的成本也更高一些,希望鴻蒙發(fā)展的越來越好。

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2013-07-18 16:09:10

自定義iOS狀態(tài)欄iOS開發(fā)iOS學(xué)習(xí)

2017-02-17 09:37:12

Android自定義控件方法總結(jié)

2012-12-24 14:42:48

iOS自定義狀態(tài)欄

2021-03-09 15:23:45

鴻蒙HarmonyOS應(yīng)用開發(fā)

2009-08-12 14:53:50

C#類型轉(zhuǎn)換函數(shù)

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2011-05-04 10:40:02

網(wǎng)頁加載進度標(biāo)題欄lephone

2015-02-12 15:33:43

微信SDK

2017-02-08 20:21:03

Windows 10Windows任務(wù)欄

2022-07-15 16:39:46

ETS導(dǎo)航欄組件

2023-02-20 15:20:43

啟動頁組件鴻蒙

2015-02-12 15:38:26

微信SDK

2009-06-17 14:13:10

Eclipse常用技巧

2009-09-03 10:08:27

JavaScript自

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2018-07-17 14:47:55

Windows 10Windows任務(wù)欄

2011-06-23 10:49:13

Qt 自定義信號

2021-10-26 10:07:02

鴻蒙HarmonyOS應(yīng)用

2021-09-15 10:19:15

鴻蒙HarmonyOS應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號