145 Star 1.4K Fork 1.6K

OpenHarmony/applications_app_samples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

自定义通知角标

介绍

本示例主要展示了设定应用的桌面图标角标的功能,使用@ohos.notificationManager 接口,进行桌面角标的设置,通知的发送,获取等。

效果预览

主页 通知发送界面 通知列表
image image image

使用说明

  1. 在使用本应用时,需安装并启动仿桌面应用
  2. 在主界面,可以看到当前应用的所有消息通知;
  3. 点击右下角Tab页签进入通知发送页面,点击对应的通知即可发送相应类型的通知,点击几次发送几条;
  4. 点击左下角Tab页签进入消息列表,可以查看刚才发送的消息,消息右边会显示消息数量,点击相应的消息可进行消息读取,取消相应通知;
  5. 回到仿桌面,可以看到角标数量,对应消息数量。

工程目录

entry/src/main/ets/
|---components
|   |---NotificationList.ets                 //通知列表控件
|   |---NotificationPublish.ets              //通知发送控件
|   |---NotificationList.ets                 //通知列表控件
|---feature
|   |---NotificationOperations.ets           // 对外提供发布通知的接口
|---entryability
|---pages
|   |---Index.ets                            // 首页
entry/src/ohosTest/ets/
|---test
|   |---Index.test.ets                       // 首页的自动化测试    
notification/src/main/ets/
|---notification
|   |---NotificationContentUtil.ets          // 封装各种通知的主体内容
|   |---NotificationManagementUtil.ets       // 封装消息列表,角标设置的接口
|   |---NotificationRequestUtil.ets          // 接收通知的主体内容,返回完整的通知
|   |---NotificationUtil.ets                 // 封装允许发布通知、发布通知、关闭通知的接口
|---util                                     // 日志文件

具体实现

  • 允许发送通知,发送通知的功能接口封装在NotificationUtil,源码参考:NotificationUtil.ets

    • 允许发送通知:在进入Index.ets 前通过notificationUtil.enableNotification()调用notification.requestEnableNotification()接口向用户请求发送通知;
    • 发送通知:通过publishNotification()封装发布通知的接口;
  • 获取应用所有消息通知、取消相关类型通知,角标管理接口封装在NotificationManagementUtil,源码参考:NotificationManagementUtil.ets

    • 获取应用所有消息通知:在constructor()构造函数中调用@ohos.notificationManager中的getActiveNotifications接口获取所有通知及相应类型通知数量,通过封装getAllNotifications()对外提供接口获取当前消息及消息数量。
    • 取消相关类型通知:通过cancelNotificationType()封装取消相关通知类型的接口;
    • 角标管理接口:通过setBadgeNumber()封装设置应用角标数量的接口,通过getBadgeNumber()封装获取当前应用角标数量的接口。
    • 添加一条通知:通过addNotification()封装接口添加一条通知到消息管理器,当发送通知的时候进行调用。
  • NotificationOperations向外提供接口,在页面中调用它们来实现功能,源码参考:NotificationOperations.ets

    • 发布通知:在Index.ets 页面中进入发送通知Tab页签,通过点击事件调用NotificationOperations中封装的对应的方法,然后从NotificationContentUtil中获取对应的主体内容content,将 content传递给NotificationRequestUtil得到完整的发布信息,最后调用NotificationUtil.publishNotification()发布内容,并调用NotificationManagementUtil的addNotification()添加一条消息;

相关权限

依赖

应用的角标依赖于仿桌面应用进行显示: 仿桌面应用

约束与限制

  1. 本示例仅支持标准系统上运行,支持设备:RK3568;
  2. 本示例仅支持API10版本SDK,版本号:4.0.7.5,镜像版本号:OpenHarmony 4.0.7.5;
  3. 本示例需要使用DevEco Studio 3.1 Release (Build Version: 3.1.0.500)才可编译运行。

下载

如需单独下载本工程,执行如下命令:

git init
git config core.sparsecheckout true
echo code/BasicFeature/Notification/CustomNotificationBadge/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openharmony/applications_app_samples.git
git@gitee.com:openharmony/applications_app_samples.git
openharmony
applications_app_samples
applications_app_samples
OpenHarmony-4.0-Beta2

搜索帮助