npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jpush-rn

v3.0.8

Published

极光推送 JPush SDK 的 React Native 封装,支持 Android 和 iOS (Fork of jpush-react-native)

Downloads

20

Readme

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 部分。