本项目基于 react-native-notifications 开发,为 React Native 鸿蒙(OpenHarmony)适配版本。
| 鸿蒙适配包版本 | 原始库版本 | 支持 RN 版本 | Autolink | 编译 API 版本 |
|---|---|---|---|---|
| 见发布记录 | 5.1.0 | 0.72+ | 是 | API12+ |
npm install @oh-rn/react-native-notificationsimport { Notifications } from 'react-native-notifications';
// 注册远程通知(鸿蒙:弹系统授权框 → 获取 Push Kit token)
Notifications.registerRemoteNotifications();
Notifications.events().registerRemoteNotificationsRegistered((event) => {
console.log('deviceToken:', event.deviceToken);
});
// 发布本地通知(点击通知经 wantAgent 回拉应用并携带原始 payload)
Notifications.postLocalNotification({
title: '会议提醒',
body: '10:30 产品评审',
payload: { category: 'SOME_CATEGORY', link: 'app://meeting/1' },
});
// 监听通知点击(含行动按钮文本输入回传)
Notifications.events().registerNotificationOpened((notification, completion, actionResponse) => {
console.log(notification.title, actionResponse?.identifier, actionResponse?.text);
completion();
});import 时使用原库名
'react-native-notifications',而非鸿蒙包名(由 RNOH alias 映射)。
平台差异:
- 远程推送走 Push Kit:
registerRemoteNotifications()先经requestEnableNotification弹系统授权框,再取 Push token(需 AGC 开通推送服务 + 真机签名;失败派发remoteNotificationsRegistrationFailed,本地通知不受影响) - 前台通知展示由系统通知中心管理,iOS 的 completion 协议(3 个 finish 方法)为空实现
abandonPermissions()跳转本应用通知设置页(API 18+),鸿蒙不支持程序化撤销授权
权限要求:
- 通知授权经运行时
requestEnableNotification系统弹窗申请,module.json5无需声明ohos.permission.* - 自定义铃声需将音频文件放入应用
resources/rawfile,sound传文件名(如'chime.wav')
| 版本 | 是否支持 Autolink |
|---|---|
| 当前版本 | 是 |
如使用版本支持 Autolink 且工程已接入,可跳过手动配置。
Manual Link 配置
说明:本模块需要同时在 C++ 侧和 ETS 侧注册 Package。
在工程根目录 oh-package.json5 添加:
{
"overrides": {
"@rnoh/react-native-openharmony": "./react_native_openharmony"
}
}打开 entry/oh-package.json5,添加:
"dependencies": {
"@oh-rn/react-native-notifications": "file:../../node_modules/@oh-rn/react-native-notifications/harmony/notifications.har"
}执行 ohpm install。
打开 entry/src/main/cpp/CMakeLists.txt,添加:
set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
add_subdirectory("${OH_MODULES}/@oh-rn/react-native-notifications/src/main/cpp" ./notifications)
target_link_libraries(rnoh_app PUBLIC notifications)打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "NotificationsPackage.h"
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<NotificationsPackage>(ctx),
};
}打开 entry/src/main/ets/RNPackagesFactory.ets,添加:
import { NotificationsPackage } from '@oh-rn/react-native-notifications/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new NotificationsPackage(ctx),
];
}| API | 描述 | 参数 | 返回值 | HarmonyOS 支持 |
|---|---|---|---|---|
| registerRemoteNotifications | 申请通知授权并注册远程推送 | options?: NotificationPermissionOptions | void | |
| postLocalNotification | 发布本地通知 | notification: Notification, id?: number | number | ✅ 完全支持 |
| cancelLocalNotification | 取消指定本地通知 | notificationId: number | void | ✅ 完全支持 |
| cancelAllLocalNotifications | 取消全部本地通知(ios 面) | 无 | void | ✅ 完全支持 |
| getInitialNotification | 获取冷启动通知(一次性消费) | 无 | Promise<Notification | undefined> | ✅ 完全支持 |
| setCategories | 注册通知类别(行动按钮) | categories: NotificationCategory[] | void | |
| setNotificationChannel | 设置通知渠道 | channel: NotificationChannel | void | |
| isRegisteredForRemoteNotifications | 查询通知开关状态 | 无 | Promise | ✅ 完全支持 |
| checkPermissions | 查询通知权限 | 无 | Promise | |
| getDeliveredNotifications | 查询已发通知 | 无 | Promise<Notification[]> | ✅ 完全支持 |
| removeDeliveredNotifications | 删除指定已发通知 | identifiers: string[] | void | ✅ 完全支持 |
| removeAllDeliveredNotifications | 删除全部已发通知 | 无 | void | ✅ 完全支持 |
| refreshToken | 刷新 Push token(android 面) | 无 | void | |
| abandonPermissions | 撤销通知授权(ios 面) | 无 | void | |
| registerPushKit | 注册 Push Kit(ios 面) | 无 | void | |
| getBadgeCount | 读取应用角标(ios 面) | 无 | Promise | |
| setBadgeCount | 设置应用角标(ios 面) | count: number | void | ✅ 完全支持 |
| finishPresentingNotification / finishHandlingAction / finishHandlingBackgroundAction | iOS completion 协议 | notificationId 等 | void | ❌ 不支持(鸿蒙无前台展示拦截回调,空实现) |
| 事件:remoteNotificationsRegistered | token 获取成功 | event: { deviceToken } | — | |
| 事件:remoteNotificationsRegistrationFailed | token 获取失败 | event: RegistrationError(code/domain/localizedDescription) | — | ✅ 完全支持(失败路径真实派发) |
| 事件:remoteNotificationsRegistrationDenied | 通知授权被拒 | 无 | — | ✅ 完全支持 |
| 事件:notificationReceived / notificationReceivedBackground | 前台/后台收到推送 | notification: Notification | — | |
| 事件:notificationOpened | 点击通知/行动按钮 | notification, completion, actionResponse? | — | ✅ 完全支持 |
| 事件:pushKitRegistered / pushKitNotificationReceived | Push Kit 注册成功/透传消息 | event | — | |
| 事件:appNotificationSettingsLinked | iOS 通知设置链接 | 无 | — | ❌ 不支持(iOS 专属概念,鸿蒙无此事件源) |
- 渠道粒度:
setNotificationChannel的 importance 映射通知槽类型(4+→社交通信、23→服务信息、01→其他)并addSlot;enableLights/vibrationPattern/groupId/lightColor 等细粒度字段鸿蒙槽位不开放 - 行动按钮:鸿蒙
NotificationActionButton仅支持 title/wantAgent/userInput;textInput文本经 inputKey 回传到actionResponse.text;每条通知最多 2 个按钮(超出截断) checkPermissions的 alert/sound/badge/lockScreen/notificationCenter 五项统一映射应用级通知开关(鸿蒙无逐项开关)getInitialNotification无冷启动通知时返回undefined(对齐 Android resolve(null) 行为)- 已发通知的
identifier统一为系统通知 id 字符串,可直接传给removeDeliveredNotifications
| API | 原因 |
|---|---|
| finishPresentingNotification / finishHandlingAction / finishHandlingBackgroundAction | 鸿蒙通知由系统通知中心直接呈现,应用无前台展示拦截/完成回调点(与原库 Android 行为一致,JS 层 completion 为 no-op) |
| appNotificationSettingsLinked 事件 | iOS「通知设置链接」专属概念,鸿蒙通知体系无此事件源 |
| abandonPermissions 程序化撤销 | 鸿蒙无撤销授权 API,最接近替代为 openNotificationSettings 跳转设置页(API 18+) |
- Push token 链路(refreshToken / registerPushKit / registerRemoteNotifications 的 token 事件)依赖 AGC 开通推送服务、
client_id配置与真机签名环境;未开通时派发remoteNotificationsRegistrationFailed(错误码如 1000900012),本地通知全链路不受影响 - 自定义铃声音频需位于应用
resources/rawfile,sound传文件名(含扩展名,如'chime.wav');未提供文件时使用系统默认音 getBadgeNumber为 API 22+ 接口,低版本设备getBadgeCount返回最近一次setBadgeCount的记录值
| 依赖 | 版本要求 |
|---|---|
| Node.js | >= 18 |
| DevEco Studio | 5.0+ / 6.0+ |
| HarmonyOS SDK | API 12+ |
1. 克隆仓库
git clone <仓库地址>
cd <仓库目录>2. 安装依赖并构建
npm install --legacy-peer-deps
npm pack # 生成 tgz 包(会自动触发 prepare 构建 JS 产物)3. 进入 example 目录,安装依赖
cd example
npm install --legacy-peer-deps4. 生成 JS Bundle
npm run dev产物:harmony/entry/src/main/resources/rawfile/bundle.harmony.js
5. 用 DevEco Studio 打开鸿蒙工程
- 打开 DevEco Studio
- 选择
example/harmony目录 - 等待 Sync 完成
6. 编译并运行 HAP
在 DevEco Studio 中点击运行按钮,将 HAP 安装到设备/模拟器。
注意:Example 中已预置插件依赖和 Package 注册,无需手动配置 Link。
- RNOH: 0.72+
- HarmonyOS SDK: API 12+
- DevEco Studio: 5.0+
- Push token 获取依赖 AGC 开通推送服务与真机签名环境,普通构建环境下
remoteNotificationsRegistered不可达(失败路径经remoteNotificationsRegistrationFailed真实暴露) - iOS completion 协议(finish 系列方法)为空实现(见「未实现功能」)
本项目基于 MIT License,详见 LICENSE 文件。