標籤:img eal 內容 install ati ref idf 技術分享 設定
這是 react native 配置極光推送使用的組件,比較常用https://github.com/jpush/jpush-react-native 先把組件地址貼出來,方便大家使用參考。如果這個大家不能配置成功,歡迎大家一起入坑交流,有問題聯絡 QQ379038610(添加備忘說明原因)
不扯沒用的,還要洗洗睡覺,直接把自己配置iOS極光的步驟給大家貼出來
1,首先大家項目環境,簽署憑證什麼都配置完畢,開始整合推送的前提下
在項目目前的目錄執行:
npm install jpush-react-native --save
rnpm link jpush-react-native
注釋:如果沒有安裝 rnpm 先 npm install rnpm 安裝 rnpm(詳情百度。。。)
2, 執行完之後,開啟 Xcode ,在 iOS 工程 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
- 在 AppDelegate.h 檔案中 匯入標頭檔
#import <RCTJPushModule.h>#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import <UserNotifications/UserNotifications.h>#endif
- 在 AppDelegate.h 檔案中 填寫如下代碼,這裡的的 appkey、channel、和 isProduction 填寫自己的
static NSString *appKey = @""; //填寫appkeystatic NSString *channel = @""; //填寫channel 一般為nilstatic BOOL isProduction = false; //填寫isProdurion 平時測試時為false ,生產時填寫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} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else {
//這裡是支援 iOS8之前的系統,不需要的可以刪掉 [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];}
- 為了在收到推送點擊進入應用能夠擷取該條推送內容需要在 AppDelegate.m didReceiveRemoteNotification 方法裡面添加 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo] 方法,注意:這裡需要在兩個方法裡面加一個是iOS7以前的一個是iOS7即以後的,如果AppDelegate.m 沒有這個兩個方法則直接複製這兩個方法,在 iOS10 的裝置則可以使用JPush 提供的兩個方法;如下所示
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // 取得 APNs 標準資訊內容 [[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); // 需要執行這個方法,選擇是否提醒使用者,有Badge、Sound、Alert三種類型可以選擇設定}// 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(); // 系統要求執行這個方法}
這些步驟 git 上面都有,但接下來的才是雞湯!!!
在 Xcode 中開啟 Push Notifications!
2)
3)
然後在 js 代碼裡面通過如下監聽回調擷取通知,最好實在項目入口檔案裡監聽
var { NativeAppEventEmitter } = require(‘react-native‘);componentDidMount (){ var subscription = NativeAppEventEmitter.addListener( ‘ReceiveNotification‘, (notification) => console.log(notification) );
}...// 千萬不要忘記忘記取消訂閱, 通常在componentWillUnmount函數中實現。subscription.remove();
前前後後在 react native 配置裡三四遍,配置並不難,特摘極光 git 上說明加上本人配置過程中的踩過的坑,供大家參考,如果有什麼不正確的地方望大家及時指出,謝謝
React Native 之極光推送jpush-react-native 手把手配置