react-native android 和ios 集成 jpush-react-native 激光推送


安裝

  

$ npm install jpush-react-native --save
# jpush-react-native 1.4.2 版本以后需要同時安裝 jcore-react-native
$ npm install jcore-react-native --save 


# 針對性的link,避免之前手動配置的其它插件重復配置造成報錯
$ react-native link jpush-react-native
$ react-native link jcore-react-native

在link 第一個的時候 會讓你輸入 appkey  可以輸入 可以不輸入,反正后面會配置

android  下配置

1.  在 android/app/build.gradle   添加下面的代碼 ,位置如圖

android {
    ...
    defaultConfig {
        applicationId "yourApplicationId" // 此處改成你在極光官網上申請應用時填寫的包名
        ...
        manifestPlaceholders = [
                JPUSH_APPKEY: "yourAppKey", //在此替換你的 APPKey
                APP_CHANNEL: "developer-default"    //應用渠道號, 默認即可
        ]
    }
}
...
dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile project(':jpush-react-native')  // 添加 jpush 依賴
    compile project(':jcore-react-native')  // 添加 jcore 依賴
    compile "com.facebook.react:react-native:+"  // From node_modules
}

如圖

 上面的  applicationId  就是包名     JPUSH_APPKEY  就是  激光推送的 appkey  ,要去激光官網申請,

 

2. 檢查 android/settings.gradle 配置有沒有包含以下內容,這里我link 后是正確的,沒有了就加上

include ':jcore-react-native'
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')

include ':jpush-react-native'
project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')

 

 

3.配置權限   android/app/src/main/AndroidManifest.xml   這里我也是link 后自己就有了

<!-- Required . Enable it you can get statistics data with channel -->
    <meta-data android:name="JPUSH_CHANNEL" android:value="${APP_CHANNEL}"/>
    <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}"/>

  

4. 加入 JPushPackage  打開 android/app/main/jave/.../MainApplication.java

 
         
  import cn.jpush.reactnativejpush.JPushPackage;   // <--   導入 JPushPackage
...
// 設置為 true 將不彈出 toast
    private boolean SHUTDOWN_TOAST = false;
    // 設置為 true 將不打印 log
    private boolean SHUTDOWN_LOG = false;

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {

        @Override
        protected boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new JPushPackage(SHUTDOWN_TOAST, SHUTDOWN_LOG)
            );
        }
    };

 

上圖我圈的地方是我自己手動配置的,這里可以參考官方文檔

android 配置到此結束

官方文檔 配置到這一步就沒,別人文章里面還有,但是我配置了出錯,就沒配置,測是了下 是可以實現推送的

 

ios 配置

1. 打開 iOS 工程,在 npm link jpush-react-native 執行完之后,RCTJPushModule.xcodeproj  RCTCoreModule.xcodeproj工程會自動添加到 Libraries 目錄里面,沒有則就手動添加

在 /node_modules/jcore-react-native/ios/  和 /node_modules/jpush-react-native/ios/  下

2.在 iOS 工程 target 的 Build Phases->Link Binary with Libraries 中加入如下庫,這些庫應該也是會自動添加的

ibz.tbd
CoreTelephony.framework
Security.framework
CFNetwork.framework
CoreFoundation.framework
SystemConfiguration.framework
Foundation.framework
UIKit.framework
UserNotifications.framework
CoreGraphics.framework
libresolv.tbd

 

3.在 iOS 工程中如果找不到頭文件可能要在 TARGETS-> BUILD SETTINGS -> Search Paths -> Header Search Paths 添加如下路徑

這一步也是在link 之后自己就有了,我沒做操作:

$(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule

 

4.在 AppDelegate.h 文件中 填寫如下代碼,這里的的 appkey、channel、和 isProduction 填寫自己的

static NSString *appKey = @"appkey";     //填寫appkey
static NSString *channel = @"nil";    //填寫channel   一般為nil
static BOOL isProduction = true;  //填寫isProdurion  平時測試時為false ,生產時填寫true

5.在AppDelegate.m 里面添加如下代碼

(1).引入依賴文件

#import "AppDelegate.h"
#import <RCTJPushModule.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

(2).在didFinishLaunchingWithOptions方法里添加

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
    
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    
    entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
    
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    
  }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    
    //可以添加自定義categories
    [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge |
                                                      UNAuthorizationOptionSound |
                                                      UNAuthorizationOptionAlert)
                                          categories:nil];
  }else {
    
    //categories 必須為nil
    [JPUSHService registerForRemoteNotificationTypes:(UNAuthorizationOptionBadge |
                                                      UNAuthorizationOptionSound |
                                                      UNAuthorizationOptionAlert)
                                          categories:nil];
  }
  
  [JPUSHService setupWithOption:launchOptions appKey:appKey
                        channel:nil apsForProduction:isProduction];

(3).  下面加的代碼,我是看別人文章上寫的,但是我上面 就有 所以沒加,但是還是列出來

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [JPUSHService registerDeviceToken:deviceToken];
}

// 取得 APNs 標准信息內容
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}

//iOS 7 Remote Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)   (UIBackgroundFetchResult))completionHandler
{
  [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}

// 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]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
  }

  completionHandler(UNNotificationPresentationOptionAlert);
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotification object:userInfo];
  }

  completionHandler();
}

自己加的部分

// add --- start -----
//這個方法是清除icon角標
- (void)applicationWillEnterForeground:(UIApplication *)application {
  [application setApplicationIconBadgeNumber:0];
  //  [application cancelAllLocalNotifications];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  
  //Optional
  
  NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
  
}

//add -- end ------

 

 

 

 

 

 

 

 

然后就可以使用了

import JPushModule from 'jpush-react-native';

//獲取RegistrationID
JPushModule.getRegistrationID((registrationId) => {
    alert(registrationId);
})

通過上面代碼 獲取  RegistrationID

然后到 激光官網 去注冊個應用 得到appkey ,然后在應用里面通過上面代碼獲取到 RegistrationID,就可以進行測試了

 

jpush-react-native配置的全部官方文檔

 

ios  android 都親測成功了的

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM