ReactNative 整合極光推送jpush-react-native(Android端)
http://www.reibang.com/p/cce27f16036d
一效拭、證書
1望蜡、在蘋果官網(wǎng)創(chuàng)建推送證書(生產(chǎn)和開發(fā))
2、在極光推送官網(wǎng)中創(chuàng)建應(yīng)用時需要配置證書(生產(chǎn)和開發(fā))
二、安裝(若參考上文已安裝請忽略)
我的react-native版本是0.42.0
npm install jcore-react-native --save
npm install jpush-react-native --save
react-native link
三带欢、配置
1、自動link(react-native link后)
(1)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
(2)RCTJPushModule.xcodeproj 工程會自動添加到 Libraries 目錄里面
(3)AppDelegate.m中增加了一些代碼
(4)target ->BuildSetting->Header Search Paths中添加
$(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule/RCTJPushModule
等等...
2症虑、手動配置
(1)target ->Capabilities->Push Notifications
(2)在plist文件中NSAppTransportSecurity添加
<dict>
<key>jpush.cn</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
(3)AppDelegate.h
static NSString *appKey = @"你的appKey";? ? //填寫appkey
static NSString *channel = @"";? ? //填寫channel? 一般為nil
static BOOL isProduction = false;? //填寫isProdurion? 平時測試時為false 连霉,生產(chǎn)時填寫true
(4)AppDelegate.m
********引入頭文件
#import <RCTJPushModule.h>
#import "JPUSHService.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
********添加@interface AppDelegate()
@interface AppDelegate()
@end
********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];
********添加方法(如果已有請刪除原有的)
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
//-----------------------------------------------------------------------
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// 取得 APNs 標(biāo)準(zhǔn)信息內(nèi)容
[[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 {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個方法沙郭,選擇是否提醒用戶佛呻,有Badge、Sound病线、Alert三種類型可以選擇設(shè)置
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
completionHandler();? // 系統(tǒng)要求執(zhí)行這個方法
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
四吓著、測試推送
參考:
http://www.reibang.com/p/e7f81b5e1807
補(bǔ)充:
"jcore-react-native":"^1.2.5",
"jpush-react-native":"^2.2.0",
最新版的jpush-react-native,react-native版本號0.52.0
最新版的jpush-react-native不需要手動配置這么多東西了送挑,但是按照文檔配置的ios端绑莺,推送發(fā)送不成功,也獲取不到RegistrationID
解決方法:
在AppDelegate.m的didFinishLaunchingWithOptions方法中增加
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
? entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
? [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];