這是 react native 配置極光推送使用的組件,比較常用https://github.com/jpush/jpush-react-native 先把組件地址貼出來(lái),方便大家使用參考。
不扯沒(méi)用的,還要洗洗睡覺(jué),直接把自己配置iOS極光的步驟給大家貼出來(lái)
1,首先大家項(xiàng)目環(huán)境,簽名證書(shū)什么都配置完畢,開(kāi)始集成推送的前提下
在項(xiàng)目當(dāng)前目錄執(zhí)行:
npm install jpush-react-native --save
rnpm link jpush-react-native
注釋?zhuān)喝绻麤](méi)有安裝 rnpm 先 npm install rnpm 安裝 rnpm(詳情百度。。。)
2, 執(zhí)行完之后,打開(kāi) Xcode ,在 iOS 工程 target 的 Build Phases->Link Binary with Libraries 中加入如下庫(kù)
libz.tbd
CoreTelephony.framework
Security.framework
CFNetwork.framework
CoreFoundation.framework
SystemConfiguration.framework
Foundation.framework
UIKit.framework
UserNotifications.framework
libresolv.tbd
在 AppDelegate.h 文件中 導(dǎo)入頭文件
#import#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import#endif
在 AppDelegate.h 文件中 填寫(xiě)如下代碼,這里的的 appkey、channel、和 isProduction 填寫(xiě)自己的
staticNSString *appKey = @"";//填寫(xiě)appkeystaticNSString *channel = @"";//填寫(xiě)channel? 一般為nilstaticBOOL isProduction =false;//填寫(xiě)isProdurion? 平時(shí)測(cè)試時(shí)為false ,生產(chǎn)時(shí)填寫(xiě)true
在AppDelegate.m 的didFinishLaunchingWithOptions 方法里面添加如下代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{if([[UIDevice currentDevice].systemVersion floatValue] >=10.0) { #ifdef NSFoundationVersionNumber_iOS_9_x_Max? ? JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];? ? entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;? ? [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];#endif}elseif([[UIDevice currentDevice].systemVersion floatValue] >=8.0) {? ? [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UIUserNotificationTypeSound |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UIUserNotificationTypeAlert)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? categories:nil];? }else{//這里是支持 iOS8之前的系統(tǒng),不需要的可以刪掉[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UIRemoteNotificationTypeSound |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UIRemoteNotificationTypeAlert)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? categories:nil];? }? [JPUSHService setupWithOption:launchOptions appKey:appKey? ? ? ? ? ? ? ? ? ? ? ? channel:channel apsForProduction:isProduction];}
在AppDelegate.m 的didRegisterForRemoteNotificationsWithDeviceToken 方法中添加 [JPUSHService registerDeviceToken:deviceToken]; 如下所示
- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {? [JPUSHService registerDeviceToken:deviceToken];}
為了在收到推送點(diǎn)擊進(jìn)入應(yīng)用能夠獲取該條推送內(nèi)容需要在 AppDelegate.m didReceiveRemoteNotification 方法里面添加 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo] 方法,注意:這里需要在兩個(gè)方法里面加一個(gè)是iOS7以前的一個(gè)是iOS7即以后的,如果AppDelegate.m 沒(méi)有這個(gè)兩個(gè)方法則直接復(fù)制這兩個(gè)方法,在 iOS10 的設(shè)備則可以使用JPush 提供的兩個(gè)方法;如下所示
- (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 {// RequiredNSDictionary * userInfo = notification.request.content.userInfo;if([notification.request.trigger isKindOfClass:[UNPushNotificationTriggerclass]]){? ? [JPUSHService handleRemoteNotification:userInfo];? ? [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];? }? completionHandler(UNNotificationPresentationOptionAlert);// 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶(hù),有Badge、Sound、Alert三種類(lèi)型可以選擇設(shè)置}// iOS 10 Support- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {// RequiredNSDictionary * userInfo = response.notification.request.content.userInfo;if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTriggerclass]]){? ? [JPUSHService handleRemoteNotification:userInfo];? ? [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];? }? completionHandler();// 系統(tǒng)要求執(zhí)行這個(gè)方法}????這些步驟 git 上面都有,但接下來(lái)的才是雞湯?。。≡?Xcode 中打開(kāi) Push Notifications!

2)

3)
然后在 js 代碼里面通過(guò)如下監(jiān)聽(tīng)回調(diào)獲取通知,最好實(shí)在項(xiàng)目入口文件里監(jiān)聽(tīng)
var{ NativeAppEventEmitter } =require('react-native');componentDidMount (){varsubscription = NativeAppEventEmitter.addListener('ReceiveNotification',? ????????(notification) =>console.log(notification)????????);}...// 千萬(wàn)不要忘記忘記取消訂閱, 通常在componentWillUnmount函數(shù)中實(shí)現(xiàn)。subscription.remove();
前前后后在 react native 配置里三四遍,配置并不難,特摘極光 git 上說(shuō)明加上本人配置過(guò)程中的踩過(guò)的坑,供大家參考,如果有什么不正確的地方望大家及時(shí)指出,謝謝
來(lái)自:http://www.cnblogs.com/yazhengwang/p/yazheng007.html