react-native 集成極光推送iOS/andriod配置

react-native 集成極光推送iOS/andriod配置

官方demojava

版本
"jcore-react-native": "^1.3.2",
    "jpush-react-native": "^2.5.3",
    "react": "16.6.0-alpha.8af6728",
    "react-native": "0.57.4",
    Xcode: version 10.2(10E125)
    Android Studio 3.3.2
時間 2019-04-10
  • npm install jpush-react-native jcore-react-native --save
  • react-native link jpush-react-native react-native link jcore-react-native
    而後會要求輸入AppKey 極光應用獲取一個AppKey
  • 解決IOS XcodeLibraries文件夾下有沒有
    RCTJpushModule.xcodproj

    RCTJcoreModule.xcodproj文件
    rnpm link jpush-react-native
    rnpm link jcore-react-native
    沒有安裝 rnpm 先 npm install rnpm -g

Android

  • android/settings.gradle
include ':jpush-react-native'
project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')
include ':jcore-react-native'
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')
  • android/app/build.gradle
android {
    ...
    defaultConfig {
        ...
        manifestPlaceholders = [
                JPUSH_APPKEY: "ebd4f2236b5698a95bd55257",
                APP_CHANNEL : "default"
        ]
        ...
    }
}
dependencies {
    implementation project(':jcore-react-native')
    implementation project(':jpush-react-native')
    ...
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
}
  • android/app/src/main/java/com/.../MainActivity.java
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;
public class MainActivity extends ReactActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // SplashScreen.show(this);  // here
        super.onCreate(savedInstanceState);
        JPushInterface.init(this); //新加
    }
    // 新加
    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    // 新加
}
  • android/app/src/main/java/com/.../MainApplication.java
import cn.jpush.reactnativejpush.JPushPackage;

public class MainApplication extends Application implements ReactApplication {
    ...
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
             
        new MainReactPackage(),
        new JPushPackage(!BuildConfig.DEBUG, !BuildConfig.DEBUG) // 新加
            
      );
    }
}
  • android/app/src/main/AndroidManifest.xml
<manifest ...>
    ...
    <meta-data
        android:name="JPUSH_APPKEY"
        android:value="${JPUSH_APPKEY}" />
    <meta-data
        android:name="JPUSH_CHANNEL"
        android:value="${APP_CHANNEL}" />
</manifest>

iOS

  • Xcode 打開,檢查 /Libraies是否有RCTJpushModule.xcodproj

    RCTJcoreModule.xcodproj,沒有上面有方法
  • target/項目名/Build Phases/Link Binary with Libraries中加入以下庫
libz.tbd
CoreTelephony.framework
Security.framework
CFNetwork.framework
CoreFoundation.framework
SystemConfiguration.framework
Foundation.framework
UIKit.framework
UserNotifications.framework
libresolv.tbd
  • UserNotifications.framework 設置status爲Optiona l
  • 項目/capacities/push notifications on 打開
  • target/項目名/Build Settings/Header Search Paths是否加入,沒有就加入
$(SRCROOT)/../node_modules/jcore-react-native/ios/RCTJCoreModule
$(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule
  • ios/Podfile
// 查看是否寫入,沒有就添加
target 'DbysDriver' do
  
 ...
  pod 'JPushRN', :path => '../node_modules/jpush-react-native'

  pod 'JCoreRN', :path => '../node_modules/jcore-react-native'
 ...
end
  • ios/項目名/AppDelegate.m
// 查看是否寫入,沒有就寫入,導入以下頭文件
#import <RCTJPushModule.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

// didFinishLaunchingWithOptions 方法裏面添加以下代碼node

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions

{
// 新加的,沒有就寫入
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];react

entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
 [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

[JPUSHService setupWithOption:launchOptions appKey:@"你的極光appkey"android

channel:nil apsForProduction:nil];

NSURL *jsCodeLocation;
...
}ios

// 這些代碼,通常不須要手動添加,會自動寫入
...git

  • (void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken

{
[JPUSHService registerDeviceToken:deviceToken];
}github

  • (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo
    // 取得 APNs 標準信息內容

{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}npm

  • (void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification

{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object: notification.userInfo];
}react-native

  • (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler

{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}api

// 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); // 須要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型能夠選擇設置
}

// 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();}...

相關文章
相關標籤/搜索