官方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
rnpm link jpush-react-native
rnpm link jcore-react-native
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 { ... 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 }
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(); } // 新加 }
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) // 新加 ); } }
<manifest ...> ... <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" /> <meta-data android:name="JPUSH_CHANNEL" android:value="${APP_CHANNEL}" /> </manifest>
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
// 查看是否寫入,沒有就添加 target 'DbysDriver' do ... pod 'JPushRN', :path => '../node_modules/jpush-react-native' pod 'JCoreRN', :path => '../node_modules/jcore-react-native' ... end
// 查看是否寫入,沒有就寫入,導入以下頭文件 #import <RCTJPushModule.h> #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
// didFinishLaunchingWithOptions 方法裏面添加以下代碼node
{
// 新加的,沒有就寫入
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
{
[JPUSHService registerDeviceToken:deviceToken];
}github
{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}npm
{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object: notification.userInfo];
}react-native
{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}api
// iOS 10 Support
{
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
{
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();}...