基于极光 jpush-react-native 的 OpenHarmony 适配版,JPush 消息推送
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 8 天前 | ||
| 7 天前 | ||
| 1 个月前 | ||
| 6 年前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 8 天前 | ||
| 9 个月前 | ||
| 2 年前 | ||
| 10 个月前 | ||
| 18 天前 | ||
| 20 天前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 10 个月前 | ||
| 1 个月前 | ||
| 8 天前 |
文档模板:v0.4.2
jpush-react-native
本项目基于 jpush-react-native 开发。
该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/jpush-react-native 版本所属关系如下:
| 三方库名称 | 三方库版本(npm地址) | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | 源码地址 |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/jpush-react-native | ~ 3.1.2 | Gitcode Releases | 0.72.* | 是 | API12+ | 3.1.1 | br_rnoh0.72 |
简介
一个基于极光推送平台的sdk支持的react-native三方库。
下载安装
进入到工程目录并输入以下命令: npm
npm install @react-native-ohos/jpush-react-native
yarn
yarn add @react-native-ohos/jpush-react-native
另外还需按照极光推送SDK-HarmonyOS集成指南完成极光推送相关的配置。
使用示例
下面的代码展示了这个库的基本使用场景:
使用时 import 的库名不变。
import React from 'react';
import { Text, View, Button } from 'react-native';
import JPush from 'jpush-react-native';
function App() {
let localNotification = { messageID: '1', title: 'add local notification', content: '测试添加一个本地通知', broadcastTime: new Date().getTime(), extras: { content: '附加内容' } };
return (
<View>
<Text style={{
color: "#555",
fontSize: 25,
marginTop: 100,
padding:20,
fontWeight: "500"
}}>jpush-react-native</Text>
<Text style={{
color: "#999",
fontSize: 15,
marginBottom:20,
padding:20,
}}>请先前往手机"设置"-->"通知和状态栏"打开测试应用的通知</Text>
<View>
<Button
key="addLocalNotification"
title="addLocalNotification"
onPress={async () => {
JPush.addLocalNotification(localNotification);
}}
/>
</View>
</View>
);
}
export default App;
Link
| 是否支持autolink | RN框架版本 | |
|---|---|---|
| ~3.1.2 | 是 | 0.72 |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/CPF-RN/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony。
1. Overrides RN SDK
为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 oh-package.json5 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
关于该字段的作用请阅读官方说明
{
"overrides": {
"@rnoh/react-native-openharmony": "^0.72.38" // ohpm 在线版本
// "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
// "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
}
}
2. 引入原生端代码
目前有两种方法:
- 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
- 直接链接源码。
方法一:通过 har 包引入(推荐)
har 包位于三方库安装路径的 `harmony` 文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@react-native-ohos/jpush-react-native": "file:../../node_modules/@react-native-ohos/jpush-react-native/harmony/jpush_react_native.har"
}
点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install
方法二:直接链接源码
如需使用直接链接源码,请参考[直接链接源码说明](https://gitcode.com/CPF-RN/usage-docs/blob/master/zh-cn/link-source-code.md)
3. 配置 CMakeLists 和引入 RNJPushPackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
...
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_END: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/jpush-react-native/src/main/cpp" ./jPushModule)
# RNOH_END: manual_package_linking_1
add_library(rnoh_app SHARED
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_jPushModule)
# RNOH_BEGIN: manual_package_linking_2
打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
+ #include "JPushModulePackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
+ std::make_shared<JPushModulePackage>(ctx)
}
4. 在 ArkTs 侧引入 RNJPushPackage
打开 entry/src/main/ets/RNPackagesFactory.ts,由于引入的是ets文件,请将RNPackagesFactory.ts文件名后缀 ts 修改为 ets,添加:
+ import {RNJPushPackage} from "@react-native-ohos/jpush-react-native/ts";
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new RNJPushPackage(ctx)
];
}
运行
点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install
然后编译、运行即可。
约束与限制
兼容性
要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。
在以下版本验证通过:
- RNOH: 0.72.96; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
接口说明
"Platform"列表示该属性在原三方库上支持的平台。
"HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| setLoggerEnable(enable: boolean): void | 设置调试模式,默认关闭状态 | function | no | iOS,Android | yes |
| init(params: {appKey: string, channel:string, production: boolean}): void | 初始化极光推送 | function | yes | iOS,Android | yes |
| getRegistrationID(callback: Callback<{ registerID: string }>): void | 调用此 API 来取得应用程序对应的 RegistrationID。 | function | no | iOS,Android | yes |
| addTags(params: {sequence: number, tags: string[]}): void | 新增标签 | function | no | iOS,Android | yes |
| updateTags(params: {sequence: number, tags: string[]}): void | 覆盖标签 | function | no | iOS,Android | yes |
| deleteTag(params: {sequence: number, tags: string[]}): void | 删除指定标签 | function | no | iOS,Android | yes |
| deleteTags(params: {sequence: number}): void | 清除所有标签 | function | no | iOS,Android | yes |
| queryTag(params:{sequence: number, tag: string}): void | 查询指定 tag 与当前用户绑定的状态 | function | no | iOS,Android | yes |
| queryTags(params: {sequence: number}): void | 查询所有标签 | function | no | iOS,Android | yes |
| setAlias(params: {sequence: number, alias: string}): void | 设置别名 | function | no | iOS,Android | yes |
| deleteAlias(params: Sequence): void | 删除别名 | function | no | iOS,Android | yes |
| queryAlias(params: {sequence: number}): void | 查询别名 | function | no | iOS,Android | yes |
| setProperties(params: {pros: any}): void | 设置推送个性化属性/更新用户指定推送个性化属性 | function | no | iOS,Android | no |
| deleteProperties(params: {pros: any}): void | 删除指定推送个性化属性 | function | no | iOS,Android | no |
| cleanProperties():void | 清除所有推送个性化属性 | function | no | iOS,Android | no |
| pageEnterTo(pageName: string): void | 进入页面 | function | no | iOS,Android | no |
| pageLeave(pageName: string): void | 离开页面 | function | no | iOS,Android | no |
| setMobileNumber(params: {sequence: number, mobileNumber: string}): void | 设置手机号码。该接口会控制调用频率,频率为 10s 之内最多 3 次 | function | no | iOS,Android | yes |
| initCrashHandler(): void | 开启 CrashLog 上报 | function | no | iOS,Android | no |
| setPowerSaveMode(enable: boolean):void | Push SDK 开启和关闭省电模式,默认为关闭 | function | no | Android | no |
| isNotificationEnabled(callback: Callback): void | 检查当前应用的通知开关是否开启 | function | no | iOS,Android | yes |
| addLocalNotification(params:{messageID: string, title: string, content: string, extras: Extra}): void | 添加一个本地通知 | function | no | iOS,Android | yes |
| removeLocalNotification(params: { messageID: string }): void | 移除指定的本地通知 | function | no | iOS,Android | yes |
| clearLocalNotifications(): void | 移除所有的本地通知 | function | no | iOS,Android | yes |
| clearAllNotifications():void | 清除所有 JPush 展现的通知(不包括非 JPush SDK 展现的) | function | no | Android | no |
| clearNotificationById(params: {notificationId:number}):void | 删除指定的通知 | function | no | Android | no |
| setMaxGeofenceNumber(params: {geoFenceMaxNumber:number}): void | 设置最多允许保存的地理围栏数量,超过最大限制后,如果继续创建先删除最早创建的地理围栏。 默认数量为10个,允许设置最小1个,最大100个。 | function | no | iOS,Android | no |
| deleteGeofence(params: { geoFenceID: string }): void | 删除指定id的地理围栏 | function | no | iOS,Android | no |
| addConnectEventListener(callback: Callback<{connectEnable: boolean }>): void | 监听连接状态 | function | no | iOS,Android | yes |
| addCommandEventListener(callback: Callback<{ cmd:number, errorCode:number, msg:string,extra:Extra}>): void | CommandEvent 事件回调 | function | no | iOS,Android | yes |
| addNotificationListener(callback: Callback <{messageID:string, title:string,content:string, extras:Extra, notificationEventType: "notificationArrived" "notificationOpened"}>):void | 远程通知事件 | function | no | iOS,Android | no |
| addLocalNotificationListener(callback: Callback <{messageID:string, title:string,content:string, extras:Extra, notificationEventType: "notificationArrived" "notificationOpened"}> | 本地通知事件 | function | no | iOS,Android | yes |
| addCustomMessageListener(callback: Callback<{messageID: string, content: string, extras: Extra;title:string}>):void | 自定义信息通知回调 | function | no | iOS,Android | yes |
| addInappMessageListener(callback: Callback<{mesageId: string, title: string, content: string, target:string, clickAction: string, extras:Extra, inappEventType: "inappShow" "inappClick"}>):void | inapp消息事件 | function | no | iOS,Android | no |
| addTagAliasListener(callback: Callback<{ code: number, tagEnable: boolean, Tags:string[],alias:string,sequence:number}>): void | tag alias事件 | function | no | iOS,Android | yes |
| addMobileNumberListener(callback: Callback<{ code: number, sequence:number }>): void | 手机号码事件 | function | no | iOS,Android | yes |
| removeListener(callback: Function): void | 移除事件 | function | no | iOS,Android | yes |
| requestPermission(): void | 在 Android 6.0 及以上的系统上,需要去请求一些用到的权限 | function | no | Android | no |
| stopPush(): void | 停止推送服务 | function | no | Android | yes |
| resumePush(): void | 恢复推送服务 | function | no | Android | yes |
| isPushStopped(callback: Callback): void | 用来检查 Push Service 是否已经被停止 | function | no | Android | yes |
| setPushTime(params: {pushTimeDays: number[], pushTimeStartHour: number, pushTimeEndHour: number}):void | 设置允许推送时间 | function | no | Android | no |
| setSilenceTime(params: {silenceTimeStartHour: number,silenceTimeStartMinute: number,silenceTimeEndHour: number, silenceTimeEndMinute: number}): void | 设置通知静默时间 | function | no | Android | no |
| setLatestNotificationNumber(params: {notificationMaxNumber: number}): void | 设置保留最近通知条数 | function | no | Android | no |
| setChannel(params: { channel: string }): void | 动态配置 channel,优先级比 AndroidManifest 里配置的高 | function | no | Android | yes |
| setDataInsightsEnable(enable: boolean): void | 设置用户洞察开关 | function | no | Android | yes |
| setBadge(params: {badge: number, appBadge: number}):void | 设置 Badge(仅支持badge) | function | no | iOS,Android | partially |
| setChannelAndSound(params: {channel:string, sound:string, channelId:string}):void | 设置通知渠道声音 | function | no | Android | no |
| setLinkMergeEnable(enable: boolean):void | 是否开启通知合并 | function | no | Android | no |
| setSmartPushEnable(enable: boolean):void | 是否开启智能推送 | function | no | iOS,Android | no |
| setGeofenceEnable(enable: boolean):void | 是否开启地理围栏 | function | no | Android | no |
| setCollectControl(enable: boolean):void | 是否开启集中控制 | function | no | iOS,Android | no |
遗留问题
其他
无
目录结构
/rntpc_jpush-react-native # 项目根目录
├── harmony # 鸿蒙适配代码
│ └─ jpush_react_native.har # har包
│ └─ jpush_react_native # 鸿蒙适配核心代码
│ └─ Index.ets # 鸿蒙适配代码入口
│ └─ src/main/ets
│ └─ RNJPushTurboModule.ets # 实现TurboModule接口
├── index.js # RN代码 入口文件
├── src/specs
│ └─ NativeJPush.ts 类型文件
├── README_en.md # 英文安装使用方法
├── README.md # 中文安装使用方法
贡献代码
使用过程中发现任何问题都可以提交 Issue,当然,也非常欢迎提交 PR 。
开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。