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

    文章

    2624

    浏览量

    65603
  • HarmonyOS
    +关注

    关注

    79

    文章

    1941

    浏览量

    29587

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

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

收藏 人收藏

    评论

    相关推荐

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

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

    鸿蒙系统三防平板怎么样

    方便地进行操作。 在实际应用鸿蒙系统三防平板表现出色。无论是在户外探险中提供导航、拍摄等功能,还是在工地作业协助进行数据分析、图纸绘
    发表于 04-12 14:26

    鸿蒙OS开发学习:【尺寸适配实现

    鸿蒙开发,尺寸适配是一个重要的概念,它可以帮助我们在不同屏幕尺寸的设备上正确显示和布局我们的应用程序。本文将介绍如何在鸿蒙开发实现尺寸
    的头像 发表于 04-10 16:05 1180次阅读
    <b class='flag-5'>鸿蒙</b>OS开发学习:【尺寸适配<b class='flag-5'>实现</b>】

    鸿蒙OS开发实例:【通知消息】

    HarmonyOS 论坛中有研发人员求助,反馈通知没有没有声音,因此在真机上验证了一下,果不其然,没有通知的提示音,后来解决办法也非常简单,在手机设置应用,将可以打开的通知提示统统改
    的头像 发表于 04-01 15:34 301次阅读
    <b class='flag-5'>鸿蒙</b>OS开发实例:【<b class='flag-5'>通知</b>消息】

    鸿蒙实战项目开发:【短信服务】

    ||---SmsModel.ets// 封装短信类 具体实现 发送短信功能在SmsModel : /* * Copyright (c) 2022 Huawei Device Co., Ltd.
    发表于 03-03 21:29

    华为鸿蒙系统怎么样 华为鸿蒙系统和安卓系统的区别

    华为鸿蒙系统是华为公司自主研发的全场景分布式操作系统,于2019年8月首次发布。鸿蒙系统不同于传统的操作
    的头像 发表于 02-02 14:54 1054次阅读

    如何在鸿蒙系统上安装Google Play

    。但是,通过以下简易步骤仍然可以在鸿蒙系统上安装Google Play。 了解鸿蒙系统和Google Play之间的不兼容性 鸿蒙
    的头像 发表于 01-31 17:13 9896次阅读

    鸿蒙OS和开源鸿蒙什么关系?

    开源鸿蒙(Open Harmony) 鸿蒙系统愿来的设计初衷,就是让所有设备都可以运行一个系统,但是每个设备的运算能力和功能都不同,所以内核
    的头像 发表于 01-30 15:44 694次阅读
    <b class='flag-5'>鸿蒙</b>OS和开源<b class='flag-5'>鸿蒙</b>什么关系?

    鸿蒙系统和安卓的区别 鸿蒙系统有什么特别之处

    了分布式架构,可以在不同设备上实现无缝连接和协同工作。而安卓系统采用的是集中式架构,设备之间的连接和协同工作相对较为困难。 鸿蒙系统具备高度灵活性和可扩展性,支持设备与设备之间的直接通
    的头像 发表于 01-18 11:45 9633次阅读

    鸿蒙原生应用/元服务开发-消息通知整体说明

    。显示当前正在进行的事件,如下载等。HarmonyOS通过ANS(Advanced Notification Service,通知系统服务)对通知类型的消息进行管理,支持多种通知类型,
    发表于 01-08 15:26

    鸿蒙原生应用/元服务开发-通知添加行为意图

    点击通知栏上的通知,即可触发WantAgent的动作。 7.最终效果呈现如下图,在实际上架的使用过程 ,API9是没法实现分享的作用的,主要原因是其它设备不一定完成了
    发表于 01-05 15:07

    鸿蒙原生应用/元服务开发-发布基础类型通知类型与接口

    基础类型通知主要应用于发送短信息、提示信息、广告推送等,支持普通文本类型、长文本类型、多行文本类型和图片类型。 表 基础类型通知的内容分类 目前系统
    发表于 01-03 14:46

    华为鸿蒙系统

    操作系统,创造一个超级虚拟终端互联的世界,将人、设备、场景有机地联系在一起,将消费者在全场景生活接触的多种智能终端,实现极速发现、极速连接、硬件互助、资源共享,用合适的设备提供场景体验。^ [1-2
    发表于 11-02 19:39

    鸿蒙操作系统的前世今生

    上层接口和分布式能力的统一。 借助以上优势, 鸿蒙操作系统实现不同终端设备之间的极速连接、硬件互助和资源共享,为不同的群体带来升级体验: 对消费者而言, 鸿蒙操作
    发表于 10-08 19:55

    FreeRTOS任务通知简介

    任务通知简介 任务通知在 FreeRTOS 是一个可选的功能,要使用任务通知的话就需要将宏configUSE_TASK_NOTIFICAT
    的头像 发表于 07-30 11:34 585次阅读