0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

鸿蒙系统中如何实现通知功能

OpenHarmony技术社区 来源:鸿蒙技术社区 作者:软通张二龙 2021-09-06 09:42 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

HarmonyOS 通过 ANS(Advanced Notification Service,即通知增强服务)系统服务来为应用程序提供发布通知的能力。

通知提供应用的即时消息或通信消息,用户可以直接删除或点击通知触发进一步的操作。

功能介绍

HarmonyOS 提供了通知功能,即在一个应用的 UI 界面之外显示的消息,主要用来提醒用户有来自该应用中的信息。

当应用向系统发出通知时,它将先以图标的形式显示在通知栏中,用户可以下拉通知栏查看通知的详细信息。

常见的使用场景:

显示接收到短消息、即时消息等。

显示应用的推送消息,如广告、版本更新等。

显示当前正在进行的事件,如播放音乐、导航、下载等。

开发指南

通知的开发指导分为创建 NotificationSlot、发布通知和取消通知等开发场景。

①创建 NotificationSlot

代码如下:

NotificationSlot slot = new NotificationSlot(“slot_001”, “slot_default”, NotificationSlot.LEVEL_MIN); // 创建notificationSlot对象

slot.setLevel(LEVEL_HIGH);//设置通知优先级

slot.setDescription(“NotificationSlotDescription”);

slot.setEnableVibration(true); // 设置振动提醒

slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);//设置锁屏模式

slot.setEnableLight(true); // 设置开启呼吸灯提醒

slot.setLedLightColor(Color.RED.getValue());// 设置呼吸灯的提醒颜色try {

NotificationHelper.addNotificationSlot(slot);

} catch (RemoteException ex) {

HiLog.warn(LABEL, “addNotificationSlot occur exception.”);

}

②发布通知

构建 NotificationRequest 对象:

request = new NotificationRequest(notificationId);

request.setSlotId(slot.getId());

调用 setContent() 设置通知的内容:

NotificationRequest.NotificationLongTextContent content = new NotificationRequest.NotificationLongTextContent();

content.setTitle(title)

.setText(messageContent);

NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(

content);

request.setGroupValue(“group information”);

request.setContent(notificationContent); // 设置通知的内容

request.setTapDismissed(!tapDismissed);//设置用户点击通知后自动消失

调用 publishNotification() 发布通知:

try {

NotificationHelper.publishNotification(request);

} catch (RemoteException ex) {

HiLog.warn(LABEL, “publishNotification occur exception.”);

}

③取消通知

如下:

调用cancelNotification()取消指定的单条通知。

try {

NotificationHelper.cancelNotification(notificationId);

} catch (RemoteException ex) {

HiLog.error(LABEL, “Exception occurred during cancelNotification invocation.”);

}

调用cancelAllNotifications()取消所有通知。

try {

NotificationHelper.cancelAllNotifications();

} catch (RemoteException ex) {

HiLog.error(LABEL, “Exception occurred during cancelAllNotifications invocation.”);

}

④通过 IntentAgent 触发通知的跳转

代码如下:

//点击通知,跳转至指定的Ability

Intent intent1 = new Intent();

// 指定要启动的Ability的BundleName和AbilityName字段

Operation operation = new Intent.OperationBuilder()

.withDeviceId(“”)

.withBundleName(“com.example.myapplication”)

.withAbilityName(“com.example.myapplication.SecondAbility”)

.build();

// 将Operation对象设置到Intent中

intent1.setOperation(operation);

List《Intent》 intentList = new ArrayList《》();

intentList.add(intent1);

// 定义请求码int requestCode = 200;

// 设置flags

List《IntentAgentConstant.Flags》 flags = new ArrayList《》();

flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);

// 指定启动一个有页面的Ability

IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,

IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);

// 获取IntentAgent实例

IntentAgent agent = IntentAgentHelper.getIntentAgent(context, paramsInfo);

//5.设置通知的IntentAgent

request.setIntentAgent(agent);

实现效果图:

附上源码:

NotificationUtils:

public class NotificationUtils {

private static final int LEVEL_HIGH = 4;

private static NotificationRequest request;

private static final int MY_MODULE = 0x00201;

private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, “MY_TAG”);

private static final boolean tapDismissed = false;

//3.调用publishNotification()发送通知

public static void publishNotification() {

try {

NotificationHelper.publishNotification(request);

} catch (RemoteException ex) {

HiLog.warn(LABEL, “publishNotification occur exception.”);

}

}

public static void initNotificationSlot(Context context, int notificationId, String title,

String messageContent) {

//1.创建NotificationSlot

NotificationSlot slot = new NotificationSlot(“slot_001”, “slot_default”, NotificationSlot.LEVEL_MIN); // 创建notificationSlot对象

slot.setLevel(LEVEL_HIGH);//设置通知优先级

slot.setDescription(“NotificationSlotDescription”);

slot.setEnableVibration(true); // 设置振动提醒

slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);//设置锁屏模式

slot.setEnableLight(true); // 设置开启呼吸灯提醒

slot.setLedLightColor(Color.RED.getValue());// 设置呼吸灯的提醒颜色

try {

NotificationHelper.addNotificationSlot(slot);

} catch (RemoteException ex) {

HiLog.warn(LABEL, “addNotificationSlot occur exception.”);

}

request = new NotificationRequest(notificationId);

request.setSlotId(slot.getId());

NotificationRequest.NotificationLongTextContent content = new NotificationRequest.NotificationLongTextContent();

content.setTitle(title)

.setText(messageContent);

NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(

content);

request.setGroupValue(“group information”);

request.setContent(notificationContent); // 设置通知的内容

request.setTapDismissed(!tapDismissed);//设置用户点击通知后自动消失

//4.返回应用,首先获取IntentAgent

Intent intent1 = new Intent();

// 指定要启动的Ability的BundleName和AbilityName字段

Operation operation = new Intent.OperationBuilder()

.withDeviceId(“”)

.withBundleName(“com.example.myapplication”)

.withAbilityName(“com.example.myapplication.SecondAbility”)

.build();

// 将Operation对象设置到Intent中

intent1.setOperation(operation);

List《Intent》 intentList = new ArrayList《》();

intentList.add(intent1);

// 定义请求码

int requestCode = 200;

// 设置flags

List《IntentAgentConstant.Flags》 flags = new ArrayList《》();

flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);

// 指定启动一个有页面的Ability

IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,

IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);

// 获取IntentAgent实例

IntentAgent agent = IntentAgentHelper.getIntentAgent(context, paramsInfo);

//5.设置通知的IntentAgent

request.setIntentAgent(agent);

}

}

MainAbilitySlice:

public class MainAbilitySlice extends AbilitySlice implements ClickedListener {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

//通知

Text notification = (Text) findComponentById(ResourceTable.Id_text_notification);

notification.setClickedListener(this);

//通知标题

String title =“测试通知”;

//通知内容文本

String content=“通知的内容已经到了!”;

NotificationUtils.initNotificationSlot(this,1,title,content);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

@Override

public void onClick(Component component) {

switch (component.getId()){

case ResourceTable.Id_text_notification:

NotificationUtils.publishNotification();

break;

}

}

}

ability_main.xml:

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout

xmlns:ohos=“http://schemas.huawei.com/res/ohos”

ohos:height=“match_parent”

ohos:orientation=“vertical”

ohos:width=“match_parent”》

《Text

ohos:id=“$+id:text_notification”

ohos:width=“match_parent”

ohos:height=“match_content”

ohos:text_size=“25fp”

ohos:top_margin=“300vp”

ohos:left_margin=“15vp”

ohos:right_margin=“15vp”

ohos:background_element=“$graphic:background_text”

ohos:text=“通知”

ohos:text_weight=“1000”

ohos:text_alignment=“horizontal_center”/》《/DirectionalLayout》

background_text.xml:

《?xml version=“1.0” encoding=“utf-8”?》《shape xmlns:ohos=“http://schemas.huawei.com/res/ohos”

ohos:shape=“rectangle”》

《corners

ohos:radius=“20”/》

《solid

ohos:color=“#a5b3a9”/》《/shape》

责任编辑:haq

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 鸿蒙系统
    +关注

    关注

    183

    文章

    2642

    浏览量

    69354
  • HarmonyOS
    +关注

    关注

    80

    文章

    2147

    浏览量

    35594

原文标题:鸿蒙OS通知功能,轻松实现!

文章出处:【微信号:gh_834c4b3d87fe,微信公众号:OpenHarmony技术社区】欢迎添加关注!文章转载请注明出处。

收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    触觉智能RK3576开发板OpenHarmony开源鸿蒙系统USB控制传输功能示例

    本文介绍OpenHarmony开源鸿蒙系统的USB控制传输功能实现及相关代码示例,基于触觉智能RK3576开发板PurplePiOH2演示。OpenHarmony的USB通信介绍
    的头像 发表于 09-30 16:31 1233次阅读
    触觉智能RK3576开发板OpenHarmony开源<b class='flag-5'>鸿蒙</b><b class='flag-5'>系统</b>USB控制传输<b class='flag-5'>功能</b>示例

    鸿蒙6.0系统星闪蓝牙入口合并

    有数码博主爆料称在华为鸿蒙 HarmonyOS 6.0 系统 Beta3 版本的部分场景操作视频可以看到,星闪和蓝牙的功能入口和功能页面已
    的头像 发表于 09-01 16:59 737次阅读

    【HarmonyOS 5】鸿蒙应用实现发票扫描、文档扫描输出PDF图片或者表格的功能

    【HarmonyOS 5】鸿蒙应用实现发票扫描、文档扫描输出PDF图片或者表格的功能 ##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿
    的头像 发表于 07-11 18:16 887次阅读
    【HarmonyOS 5】<b class='flag-5'>鸿蒙</b>应用<b class='flag-5'>实现</b>发票扫描、文档扫描输出PDF图片或者表格的<b class='flag-5'>功能</b>

    关于BLE通知值的通知长度问题求解

    FreeRtos 的 streambuffer 的压力。 如果 notify 支持每次通知大小大于 244 的数组,我需要做哪些修改才能实现每次通知大小为 512 或 1024 的
    发表于 07-01 06:58

    【HarmonyOS 5】桌面快捷方式功能实现详解

    【HarmonyOS 5】桌面快捷方式功能实现详解 ##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财# 一、前言 在移动应用开发
    的头像 发表于 06-21 16:42 1805次阅读
    【HarmonyOS 5】桌面快捷方式<b class='flag-5'>功能</b><b class='flag-5'>实现</b>详解

    【HarmonyOS 5】鸿蒙的UIAbility详解(三)

    【HarmonyOS 5】鸿蒙的UIAbility详解(三) ##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财# 一、前言 本文是
    的头像 发表于 06-14 22:32 496次阅读

    鸿蒙Next实现瀑布流布局

    高度或宽高比,减少动态测量开销 合理实现懒加载:对于非首屏内容或图片资源,一定要实现懒加载 渐进式增强体验:先确保基础功能可用,再添加动画和交互效果 测试与优化:在不同设备上测试性能表现,针对卡顿问题进行专项优化 遵循
    发表于 06-10 14:17

    HarmonyOS NEXT应用开发-Notification Kit(用户通知服务)通知类型、级别与渠道

    :SystemCapability.Notification.Notification 名称 值 说明 LEVEL_NONE 0 表示关闭通知功能。 LEVEL_MIN 1 表示通知功能
    发表于 06-09 14:39

    HarmonyOS NEXT应用开发-Notification Kit(用户通知服务)更多系统能力

    = _NotificationActionButton 描述通知显示的操作按钮。 系统能力: SystemCapability.Notification.Notification 类型 说明
    发表于 06-04 15:43

    国产操作系统加速崛起——鸿蒙电脑补齐鸿蒙生态最重要拼图

    实现重要突破。 “鸿蒙电脑的推出,是鸿蒙操作系统生态版图得以完整的关键一步。”浙江大学传媒与国际文化学院常务副院长方兴东对科技日报记者说,“目前全球只有
    的头像 发表于 05-21 11:41 507次阅读

    DevEco Studio AI辅助开发工具两大升级功能 鸿蒙应用开发效率再提升

    HarmonyOS应用的AI智能辅助开发助手——CodeGenie,该AI助手深度集成在DevEco Studio,提供鸿蒙知识智能问答、鸿蒙ArkTS代码补全/生成和万能卡片生成等功能
    发表于 04-18 14:43

    【「鸿蒙操作系统设计原理与架构」阅读体验】01-初始华为鸿蒙

    ,然后介绍了鸿蒙操作系统出现的原因,在国产化的地位以及标志性的意义,同时也介绍了关于鸿蒙操作系统的基础设计理念以及
    发表于 01-25 11:05

    AIGC入门及鸿蒙入门

    模型,能够生成与给定文本描述相符的图像。 鸿蒙系统入门 1. 基础知识: 鸿蒙系统(HarmonyOS)是华为推出的一款分布式操作系统,旨
    发表于 01-13 10:32

    开源鸿蒙系统外设指纹仪模块功能演示#OpenHarmony

    鸿蒙系统
    深圳市视美泰技术股份有限公司
    发布于 :2024年12月17日 10:45:58

    名单公布!【书籍评测活动NO.53】鸿蒙操作系统设计原理与架构

    的底层设计逻辑出发,针对不同关键子系统的目标功能实现路径做实际分析解读,帮助开发者理解鸿蒙操作系统的底层逻辑,开发更适合
    发表于 12-16 15:10