React Native 之極光推送jpush-react-native 手把手配置

這是 react native 配置極光推送使用的組件,比較常用https://github.com/jpush/jpush-react-native 先把組件地址貼出來,方便大家使用參考。

不扯沒用的,還要洗洗睡覺,直接把自己配置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 = @"";     //填寫appkey
static NSString *channel = @"";    //填寫channel   一般為nil
static 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 *)application
didRegisterForRemoteNotificationsWithDeviceToken:(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 上說明加上本人配置過程中的踩過的坑,供大家參考,如果有什么不正確的地方望大家及時指出,謝謝

 

來自:http://www.cnblogs.com/yazhengwang/p/yazheng007.html

 

 本文由用戶 zclv8057 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!