第7章_HarmonyOS 图解 Ability公共事件与通知

发布时间:2026/7/24 5:10:00
第7章_HarmonyOS 图解 Ability公共事件与通知 第7章 HarmonyOS 图解公共事件与通知HarmonyOS 图解 学习系统 | 阶段二进阶实战期建议学习时长1-2 周学习目标序号能力1掌握公共事件服务CES的发布与订阅机制2能区分有序事件、无序事件和粘性事件3掌握通知服务ANS的创建和发布流程4了解通知的多种样式和渠道管理内容讲解7.1 公共事件服务 (CES)想象一个社区公共事件 (CES) 社区广播站——“今晚停水通知”系统事件或者张三家孩子满月请吃席自定义事件。所有订阅了广播的人都能收到可以有序地排队处理有序事件或者谁收到谁处理无序事件。有些广播还会粘在广播站新来的人也能听到之前的广播内容粘性事件。通知 (ANS) 手机收到的一条私信——“您的快递到了”。它会出现在通知栏你可以点击查看也可以直接划掉忽略。HarmonyOS 通过 CESCommon Event Service为应用提供订阅、发布、退订公共事件的能力。公共事件分为系统公共事件和自定义公共事件。事件类型类型说明典型场景无序事件订阅者同时收到无先后顺序天气变化通知、自定义业务事件有序事件订阅者按优先级顺序接收处理短信到达、电量变化粘性事件新订阅者也能收到之前发布的事件系统启动完成、电量信息发布自定义公共事件// 创建 Intent 并设置 ActionIntentintentnewIntent();intent.setAction(com.example.myapp.CUSTOM_EVENT);intent.setParam(message,Hello from publisher!);intent.setParam(timestamp,System.currentTimeMillis());// 封装为 CommonEventData 并发布CommonEventDataeventDatanewCommonEventData(intent);CommonEventManager.publishCommonEvent(eventData);// 发布粘性事件新订阅者也能收到CommonEventManager.publishCommonEvent(eventData,CommonEventPublishInfo.PUBLISH_STICKY);订阅公共事件// 创建订阅信息CommonEventSubscribeInfosubscribeInfonewCommonEventSubscribeInfo(com.example.myapp.CUSTOM_EVENT);// 设置优先级有序事件需要subscribeInfo.setPriority(100);// 创建订阅者并订阅MySubscribersubscribernewMySubscriber(subscribeInfo);CommonEventManager.subscribeCommonEvent(subscriber);事件接收处理classMySubscriberextendsCommonEventSubscriber{publicMySubscriber(CommonEventSubscribeInfoinfo){super(info);}OverridepublicvoidonReceiveEvent(CommonEventDatacommonEventData){StringmsgcommonEventData.getIntent().getStringParam(message);longtimestampcommonEventData.getIntent().getLongParam(timestamp,0);HiLog.info(TAG,Received: msg at timestamp);}}订阅系统公共事件系统提供了大量预定义的公共事件 Action常见的包括系统 Action说明CommonEventSupport.COMMON_EVENT_SCREEN_ON屏幕亮起CommonEventSupport.COMMON_EVENT_SCREEN_OFF屏幕熄灭CommonEventSupport.COMMON_EVENT_BATTERY_CHANGED电量变化CommonEventSupport.COMMON_EVENT_NETWORK_CHANGED网络状态变化CommonEventSupport.COMMON_EVENT_PACKAGE_ADDED应用安装CommonEventSupport.COMMON_EVENT_PACKAGE_REMOVED应用卸载退订公共事件// 退订公共事件在 onStop 或 onDestroy 中调用CommonEventManager.unsubscribeCommonEvent(subscriber);7.2 高级通知服务 (ANS)通知是应用在 UI 界面之外显示的消息用于提醒用户。通知支持多种样式普通文本、长文本、图片、社交、多行文本和媒体样式。创建通知渠道在发布通知之前必须先创建通知渠道// 创建通知渠道NotificationSlotslotnewNotificationSlot(slot_id,默认渠道,NotificationSlot.LEVEL_DEFAULT);slot.setDescription(应用默认通知渠道);NotificationHelper.addNotificationSlot(slot);通知等级说明等级常量说明最低LEVEL_MIN不弹出横幅仅在通知栏显示低LEVEL_LOW不发出提示音默认LEVEL_DEFAULT发出提示音和振动高LEVEL_HIGH发出提示音、振动并横幅弹出发布通知// 创建通知请求NotificationRequestrequestnewNotificationRequest(1001);request.setSlotId(slot_id);// 设置通知内容NotificationRequest.NotificationNormalContentcontentnewNotificationRequest.NotificationNormalContent();content.setTitle(新消息);content.setText(您收到一条新消息);content.setAdditionalText(来自 HarmonyOS 学习系统);// 包装内容NotificationRequest.NotificationContentnotificationContentnewNotificationRequest.NotificationContent(content);request.setContent(notificationContent);// 发布通知NotificationHelper.publishNotification(request);取消通知// 取消指定通知NotificationHelper.cancelNotification(1001);// 取消该应用所有通知NotificationHelper.cancelAllNotifications();订阅通知事件应用可以订阅通知的发布、取消、点击等事件// 订阅通知事件NotificationSubscribeInfoinfonewNotificationSubscribeInfo();info.setBundleName(com.example.myapp);NotificationSubscribersubscribernewNotificationSubscriber(info){OverridepublicvoidonSubscribe(NotificationSlotnotificationSlot){// 订阅成功}OverridepublicvoidonUnsubscribe(Strings){// 取消订阅}OverridepublicvoidonUpdate(Notificationnotification,Strings){// 收到通知更新}OverridepublicvoidonDisturbModeChange(intdisturbMode){// 免打扰模式变化}};代码速查卡操作API说明发布事件CommonEventManager.publishCommonEvent()传入 CommonEventData订阅事件CommonEventManager.subscribeCommonEvent()传入 CommonEventSubscriber退订事件CommonEventManager.unsubscribeCommonEvent()在 onStop/onDestroy 中调用创建通知渠道NotificationHelper.addNotificationSlot()必须先创建才能发通知发布通知NotificationHelper.publishNotification()传入 NotificationRequest取消通知NotificationHelper.cancelNotification()指定 id 取消取消所有通知NotificationHelper.cancelAllNotifications()清除该应用所有通知设置通知等级NotificationSlot.setLevel()LEVEL_MIN/LOW/DEFAULT/HIGH与 Android/iOS 对比特性HarmonyOSAndroidiOS事件总线CommonEventService (CES)BroadcastReceiverNotificationCenter有序事件支持优先级排序支持android:priority不支持粘性事件支持支持已废弃不支持通知渠道NotificationSlot (必须创建)NotificationChannel (Android 8)不需要通知管理NotificationHelperNotificationManagerUNUserNotificationCenter⚠️ 踩坑回忆录做通知功能时我直接调用了publishNotification()结果通知发不出去也没有任何报错。后来才知道HarmonyOS 要求必须先创建通知渠道addNotificationSlot()否则通知会被静默丢弃。这跟 Android 8.0 的 NotificationChannel 要求类似但 HarmonyOS 的报错信息更不明显连个日志都没有。还有一点要注意publishCommonEvent()不需要权限就可以发布自定义事件但如果你想发布系统级别的有序事件或者粘性事件可能需要额外的权限声明。订阅系统公共事件时也要注意有些系统事件需要ohos.permission.RECEIVER_SYS_EVENT权限。必做实操任务序号任务难度1实现一个应用内组件间的自定义事件通信★★☆2订阅屏幕亮/灭的系统公共事件并打印日志★★☆3实现通知渠道的创建和通知的发布★★☆4尝试发布不同等级的通知观察通知栏的表现差异★★☆5实现通知的点击事件处理点击通知跳转到指定页面★★★学习检查清单能说出 CES 和 ANS 的全称和作用能区分有序事件、无序事件和粘性事件能实现自定义公共事件的发布和订阅能说出至少 3 个系统公共事件的 Action能实现通知渠道的创建和通知的发布理解通知等级的含义知道退订公共事件的正确时机进阶方向学习通知的各种样式长文本、图片、媒体等实现带进度条和操作按钮的通知了解免打扰模式对通知的影响

相关新闻

最新新闻

日新闻

周新闻

月新闻