jpush-rn
TypeScript icon, indicating that this package has built-in type declarations

3.0.8 • Public • Published

JPush-RN

极光推送 JPush SDK 的 React Native 封装,支持 AndroidiOS,Fork 自 jpush-react-native


为啥自己维护一份呢?因为官方 jpush-react-native 项目做得比较早,各方面更新比较慢。最初我跟着 jpush-react-native 文档配置,很多东西没生效,最后去看官方 Android SDK 集成iOS SDK 集成文档才恍然大悟。我把这部分的配置说明写在本文档下方,希望对你有帮助。

如果有一点 Android 和 iOS 开发经验,建议也去看下官方的客户端 SDK文档,很多问题在里面都可以找到答案。

有疑问或者本项目存在问题,请在 Issues 中反馈,非常感谢。

备注: 对 Android 14 (API 34) 做了适配。

1. 安装

依赖到 JCore SDK,所以 jcore-rn 也需要安装。

NPM:

npm i jcore-rn jpush-rn

YARN:

yarn add jcore-rn jpush-rn

2. SDK 版本

2.1. Android

Android 使用 mavenCentral 自动集成 JPush SDK,当前版本为 5.2.2

2.2. iOS

iOS 使用 Cocoapods 自动导入 JPush SDK,当前版本为 5.2.0

3. 配置

3.1 Android

3.1.1 链接静态库

React Native 0.60 及以上版本是自动链接的,无需理会。如果你的项目是 0.59 及以下版本,请参考 jpush-react-native 中的手动配置方式。

3.1.2 添加 JPush 配置

  • 修改 android/build.gradle,添加以下内容:
android {
    defaultConfig {
        applicationId "yourApplicationId"   // 在此替换你的应用包名
        ...
        manifestPlaceholders = [
            JPUSH_APPKEY: "yourAppKey",     // 在此替换你的 APPKey
            JPUSH_CHANNEL: "yourChannel"    // 在此替换你的 channel
        ]
    }
}
  • 修改 android/app/src/main/AndroidManifest.xml,添加以下内容:
<meta-data
  android:name="JPUSH_CHANNEL"
  android:value="${JPUSH_CHANNEL}" />
<meta-data
  android:name="JPUSH_APPKEY"
  android:value="${JPUSH_APPKEY}" />
    
<!-- Required since 5.2.0, 用于接收应用内消息等 -->
<service
    android:name="cn.jiguang.plugins.push.receiver.JPushModuleReceiver"
    android:enabled="true"
    android:exported="false" >
    <intent-filter>
        <action android:name="cn.jpush.android.intent.SERVICE_MESSAGE" />
        <category android:name="${applicationId}" />
    </intent-filter>
</service>
  • 修改 android/app/proguard-rules.pro,添加以下内容:
-dontoptimize
-dontpreverify

-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-keep class * extends cn.jpush.android.service.JPushMessageService { *; }

-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }

3.1.3 重新编译项目

正常情况下,执行 react-native run-android 编译运行即可。

如果报错,可以尝试清除缓存,再重新编译:

# 清除 Android 编译缓存
cd android
./gradlew clean
rm -rf .gradle
cd ..

# 删除 node_modules 目录,重新安装依赖
rm -rf node_modules
npm install # 或者 yarn install

3.2 iOS

3.2.1 链接静态库

React Native 0.60 及以上版本,只需执行下面的命令即可。如果你的项目是 0.59 及以下版本,请参考 jpush-react-native 中的手动配置方式。

cd ios
pod install

如果报错,可尝试执行命令:

pod deintegrate

3.2.2 添加 JPush 配置

引入头文件

将以下代码添加到 AppDelegate.m 引用头文件的位置

// 引入 jpush-rn 模块头文件
#import <RCTJPushModule.h>
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用 idfa 功能,引入所需要的头文件(可选)
// #import <AdSupport/AdSupport.h>

添加初始化 APNs 代码

将以下代码添加到 AppDelegate.m 文件的 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中:

// Required, 初始化 APNs
JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
if (@available(iOS 12.0, *)) {
  entity.types = JPAuthorizationOptionAlert | JPAuthorizationOptionBadge |
                 JPAuthorizationOptionSound |
                 JPAuthorizationOptionProvidesAppNotificationSettings;
}
[JPUSHService
    registerForRemoteNotificationConfig:entity
                               delegate:(id<JPUSHRegisterDelegate>)self];

注册 APNs 成功回调方法,并上报 DeviceToken

将以下代码添加到 AppDelegate.m 文件中:

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

  /// Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}

添加处理 APNs 通知回调方法

将以下代码添加到 AppDelegate.m 文件中:

// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center
    openSettingsForNotification:(UNNotification *)notification {
  if (notification && [notification.request.trigger
                          isKindOfClass:[UNPushNotificationTrigger class]]) {
    // 从通知界面直接进入应用
  } else {
    // 从通知设置界面进入应用
  }
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center
        willPresentNotification:(UNNotification *)notification
          withCompletionHandler:(void (^)(NSInteger))completionHandler {
  NSDictionary *userInfo = notification.request.content.userInfo;
  if ([notification.request.trigger
          isKindOfClass:[UNPushNotificationTrigger class]]) {
    // APNs 通知
    [JPUSHService handleRemoteNotification:userInfo];
    // 传递给 React Native
    [[NSNotificationCenter defaultCenter]
        postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT
                      object:userInfo];
  } else {
    // 本地通知
    // 传递给 React Native
    [[NSNotificationCenter defaultCenter]
        postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT
                      object:userInfo];
  }
  // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  completionHandler(UNNotificationPresentationOptionAlert);
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center
    didReceiveNotificationResponse:(UNNotificationResponse *)response
             withCompletionHandler:(void (^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;
  if ([response.notification.request.trigger
          isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
    [[RCTJPushEventQueue sharedInstance]._notificationQueue
        insertObject:userInfo
             atIndex:0];
    // 传递给 React Native
    [[NSNotificationCenter defaultCenter]
        postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT
                      object:userInfo];
  } else {
    // 本地通知
    // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
    [[RCTJPushEventQueue sharedInstance]._localNotificationQueue
        insertObject:userInfo
             atIndex:0];
    // 传递给 React Native
    [[NSNotificationCenter defaultCenter]
        postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT
                      object:userInfo];
  }
  completionHandler();
}

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:
              (void (^)(UIBackgroundFetchResult))completionHandler {
  // Required, iOS 7 Support
  [JPUSHService handleRemoteNotification:userInfo];
  // 传递给 React Native
  [[NSNotificationCenter defaultCenter]
      postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT
                    object:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo {

  // Required, For systems with less than or equal to iOS 6
  [JPUSHService handleRemoteNotification:userInfo];
}

// 自定义消息
- (void)networkDidReceiveMessage:(NSNotification *)notification {
  NSDictionary *userInfo = [notification userInfo];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:J_CUSTOM_NOTIFICATION_EVENT
                    object:userInfo];
}

关于应用内消息

官方文档中有提到,应用内消息默认不展示,可通过获取接口自行编码处理。

4. 点击通知,打开 URL/DeepLink

如果想点击通知打开 URL 或 DeepLink,可以参考 React Native 官方文档的 APIs - Linking 部分。

Package Sidebar

Install

npm i jpush-rn

Weekly Downloads

57

Version

3.0.8

License

ISC

Unpacked Size

298 kB

Total Files

59

Last publish

Collaborators

  • atypiape