ReactNative 整合極光推送jpush-react-native(IOS端)

ReactNative 整合極光推送jpush-react-native(Android端)

http://www.itdecent.cn/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.itdecent.cn/p/e7f81b5e1807


補充:

"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];


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容