zl程序教程

您现在的位置是:首页 >  其他

当前栏目

HarmonyOS自定义常用通知栏

2023-03-15 22:45:21 时间

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

前言

通知(Notification)旨在让用户以合适的方式及时获得有用的新消息,帮助用户高效地处理任务。

系统为开发者提供了不同种类的通知样式模板可以使用,开发者也可以根据自己需要自定义通知样式。

HarmonyOS通知相关类

与通知相关的主要逻辑在NotificationSlotNotificationRequestNotificationHelper这三个类中,那这三个类都有什么作用呢?下面为大家逐一介绍。

1. NotificationSlot

这是一个定义通知的主题类,它可以设置通知的特征集合,包括通知到来时的提示音调、振动、锁屏显示以及设置通知的重要级别等。一般可以在应用的AbilityPackage里设置,用以统一整个应用的通知主题特征,一个应用也可以关联多个不同NotificationSlot。

重点说下NotificationSlot的几个重要级别,也可以查看官方Api文档:

  • LEVEL_NONE: 表示通知不发布。
  • LEVEL_MIN:表示通知可以发布,但是不显示在通知栏,不自动弹出,无提示音;该级别不适用于前台服务的场景。
  • LEVEL_LOW:表示通知可以发布且显示在通知栏,不自动弹出,无提示音。
  • LEVEL_DEFAULT:表示通知发布后可在通知栏显示,不自动弹出,触发提示音。
  • LEVEL_HIGH:表示通知发布后可在通知栏显示,自动弹出,触发提示音。

代码示例

  1. // 创建notificationSlot对象 
  2. NotificationSlot slot = new NotificationSlot(id, "testSlot", NotificationSlot.LEVEL_HIGH); 
  3. slot.setDescription("create notificationSlot description"); 
  4. slot.setLevel(NotificationSlot.LEVEL_HIGH); 
  5. // 设置振动提醒 
  6. slot.setEnableVibration(true); 
  7. // 设置锁屏模式 
  8. slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC); 
  9. // 设置开启呼吸灯提醒 
  10. slot.setEnableLight(true); 

  11. // 设置呼吸灯的提醒颜色 
  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(); 

关于设置呼吸灯说明,由于手上只有一部P40Pro不带呼吸灯,所以无法验证实际效果。

2. NotificationRequest

NotificationRequest是通知最主要的部分,主要设置通知的样式,HarmonyOS主要提供了6种类型的样式:普通文本NotificationNormalContent、长文本NotificationLongTextContent、图片NotificationPictureContent、多行NotificationMultiLineContent、社交NotificationConversationalContent、媒体NotificationMediaContent。另外还有一种自定义样式,这些会在后面具体介绍。

虽然通知中提供了各种属性的设置,但是一个通知对象,有几个属性是必须要设置的,其他的属性均是可选的,必须设置的属性如下:

  • 小图标,使用setLittleIcon()方法设置。
  • 标题,使用setTitle()方法设置。
  • 文本内容,使用setText()方法设置。

调用setIntentAgent()设置通知可以触发的事件

  1. Intent intent = new Intent(); 
  2. // 指定要启动的Ability的BundleName和AbilityName字段 
  3. // 将Operation对象设置到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. // 设置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

该静态类主要是管理通知,提供了发布、更新、删除通知等静态方法;

主要接口如下:

  • publishNotification(NotificationRequest request),发布通知,当NotificationRequest被设置后,通过该接口去发布通知;
  • cancelNotification(int notificationId),取消通知,每个NotificationRequest创建时都必须有一个notificationId,可以通过这个接口取消创建的通知;
  • cancelAllNotifications(),取消之前发布的所有通知;
  • addNotificationSlot(NotificationSlot slot),创建一个NotificationSlot;
  • setNotificationBadgeNum(int num),设置通知的角标;

通知的代码结构基本就是围绕这三个类来构建的,其中最重要的就是NotificationRequest这个类,整个HarmonyOS各种酷炫通知都是基于这个类来定制的,所以研究通知,不如说其实就是研究NotificationRequest。下面就来介绍下HarmonyOS官方提供的6中样式以及自定义样式,基本也就包含日常所有的通知需求了。

HarmonyOS通知样式

1. 普通文本NotificationNormalContent

这是通知最基础也是最常用的样式,对应设置NotificationRequest.setLittleIcon()、NotificationNormalContent.setTitle()、NotificationNormalContent.setText();

效果图

【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  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. // 设置通知的内容 
  12. request.setContent(notificationContent); 
  13. request.setIntentAgent(intentAgent); 
  14. try { 
  15.     NotificationHelper.publishNotification(request); 
  16. } catch (RemoteException e) { 
  17.     e.printStackTrace(); 
  1. /** 
  2.  * 图片转换工具方法 
  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技术社区
【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  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. // 设置通知的内容 
  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技术社区
【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  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. // 设置通知的内容 
  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技术社区
【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  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("基础工资: 210000"
  11.                 .addSingleLine("加班补助: 97630"
  12.                 .addSingleLine("餐补: 900"
  13.                 .addSingleLine("交通补助: 1200"
  14.                 .addSingleLine("出差补助: 9800"
  15.                 .setExpandedTitle("张学友工资单"); 
  16. NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(multiLineContent); 
  17. // 设置通知的内容 
  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技术社区
【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  1. ArrayList<String> arrayListStr = new ArrayList<>(); 
  2. arrayListStr.add("结婚以后两个人在一起最重要的是什么?"); 
  3. arrayListStr.add("你是如何走出人生的阴霾的?"); 
  4. arrayListStr.add("怎么不回复我??我生气了!!"); 
  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. // 设置通知的内容 
  27. request.setContent(notificationContent); 
  28. request.setIntentAgent(intentAgent); 
  29. try { 
  30.     NotificationHelper.publishNotification(request); 
  31. } catch (RemoteException e) { 
  32.     e.printStackTrace(); 

6. 媒体NotificationMediaContent

具体媒体会话管理,请参考开发-媒体会话管理开发指导

效果图

【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  1. // 按钮文字设置无效,图标颜色也不生效,默认都是灰色 
  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后,此通知可以与关联的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. // 设置通知的内容 
  24. request.setContent(notificationContent); 
  25. try { 
  26.     NotificationHelper.publishNotification(request); 
  27. } catch (RemoteException e) { 
  28.     e.printStackTrace(); 

7. 自定义通知样式

效果图

【中软国际】HarmonyOS自定义常用通知栏-鸿蒙HarmonyOS技术社区

代码示例

  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的父布局中设置一个卡片属性“ohos:remote="true"”,否则自定义效果无法出现 
  13. ComponentProvider componentProvider = new ComponentProvider(layoutId, context); // 创建ComponentProvider对象 
  14. // componentProvider.setString(ResourceTable.Id_ongoing_card_text, "setText""TextContent"); // 设置布局中的文本内容 
  15. request.setCustomView(componentProvider); 
  16. request.setIntentAgent(intentAgent); 
  17.  
  18. try { 
  19.     NotificationHelper.publishNotification(request); 
  20. } catch (RemoteException e) { 
  21.     e.printStackTrace(); 

上面这些就是通知常用的几种效果,有很多其他的属性没有在demo中展示出来,比如角标、通知栏进度条等,这些都有属性可以设置的,相比其他移动操作系统,鸿蒙的通知样式更加丰富全面也更加统一,相对来说开发的成本也更高一些,希望鸿蒙发展的越来越好。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com