React-Native-iOS推送集成

首先是iOS推送流程,這篇文章有了比較詳細(xì)的描述,大家可以了解一下
iOS推送流程

而本文主要是針對(duì)的需求是在已擁有自己的推送服務(wù)器的情況下,怎么在js端獲取已注冊(cè)的device token,并將其交由后臺(tái)去保存。今后的推送流程就是,當(dāng)有消息需要推送時(shí),后臺(tái)調(diào)用推送服務(wù)器相應(yīng)接口傳遞消息,推送服務(wù)器將消息推給APNS(蘋果推送服務(wù)器)。APNS再通過(guò)注冊(cè)的device token推給相應(yīng)手機(jī)。

如果想直接集成三方推送平臺(tái)的話,可參考以下文章
React-Native-iOS極光推送

1.前置準(zhǔn)備

  • iOS推送證書(shū)及App相應(yīng)描述文件
    在擁有Apple開(kāi)發(fā)者賬號(hào)的情況下,可前往蘋果開(kāi)發(fā)者網(wǎng)站進(jìn)行申請(qǐng),具體流程可見(jiàn)以下優(yōu)秀文章:
    手把手教學(xué)iOS推送證書(shū)申請(qǐng)

2.導(dǎo)入相應(yīng)庫(kù)

1.首先手動(dòng)鏈接PushNotificationIOS的庫(kù):

step1 添加項(xiàng)目依賴

node_modules/react-native/Libraries/PushNotificationIOS/RCTPushNotification.xcodeproj文件拖到Xcode界面的Library中

Snip20170704_1.png

step2 添加靜態(tài)庫(kù)

并在Xcode的Link Binary With Libraries中添加libRCTPushNotification.a

Snip20170704_2.png

2.打開(kāi)推送的相關(guān)配置

1.開(kāi)啟推送功能

Snip20170704_4.png

2.開(kāi)啟后臺(tái)推送

Snip20170704_5.png

3.替換項(xiàng)目文件

更改項(xiàng)目中的AppDelegate.m文件

step1 引入依賴庫(kù)、設(shè)置代理

#import <React/RCTPushNotificationManager.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@interface AppDelegate()<UNUserNotificationCenterDelegate>

@end

step2 添加代碼

在方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中加入以下代碼(目的:根據(jù)系統(tǒng)版本注冊(cè)deviceToken

  if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
    //iOS10特有
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    // 必須寫代理,不然無(wú)法監(jiān)聽(tīng)通知的接收與點(diǎn)擊
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
      if (granted) {
        // 點(diǎn)擊允許
        NSLog(@"注冊(cè)成功");
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
          NSLog(@"%@", settings);
        }];
      } else {
        // 點(diǎn)擊不允許
        NSLog(@"注冊(cè)失敗");
      }
    }];
  }else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
    //iOS8 - iOS10
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
    
  }else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
    //iOS8系統(tǒng)以下
    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
  }
  // 注冊(cè)獲得device Token
  [[UIApplication sharedApplication] registerForRemoteNotifications];

在上個(gè)方法后添加以下方法

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
  [RCTPushNotificationManager didReceiveRemoteNotification:notification];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
}

在React-Native的JS中獲取推送相應(yīng)參數(shù)

1.導(dǎo)入PushNotificationIOS

import PushNotificationIOS from 'react-native';

2.添加相應(yīng)監(jiān)聽(tīng)事件

   //界面加載完成時(shí) 注冊(cè)監(jiān)聽(tīng)事件 
     componentDidMount() {
          // Add listener for push notifications
         PushNotificationIOS.addEventListener('notification', this._onNotification);
       // Add listener for local notifications
      PushNotificationIOS.addEventListener('localNotification', this._onLocalNotification);
       // Add listener for deviceToken registered
      PushNotificationIOS.addEventListener('register', this._register);
     }

   //界面即將消失時(shí) 注銷監(jiān)聽(tīng)事件 
     componentWillUnMount() {
       // Remove listener for notifications
         PushNotificationIOS.removeEventListener('notification', this._onNotification);
      PushNotificationIOS.removeEventListener('localNotification', this._onLocalNotification);
      PushNotificationIOS.removeEventListener('register', this._register);
    }

   //receive remote notification
   _onNotification(notification) {
   
   }
   //receive local notification
   _onLocalNotification(notification){
         
   }
   //獲取device token
   _register(deviceToken) {
       //使用window保存下devicetoken
       window.iOSDeviceToken = deviceToken;
   }

3.將設(shè)備device token 交由后臺(tái)處理
這個(gè)可能要具體問(wèn)題具體分析,根據(jù)相應(yīng)業(yè)務(wù)需求.
但device token 主要用途是當(dāng)Provider(本地推送服務(wù)器)需要推送消息給APNS(蘋果推送服務(wù)器)時(shí),傳遞相應(yīng)的device token.APNS找到設(shè)備編號(hào),進(jìn)行推送。

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

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

  • 極光推送: 1.JPush當(dāng)前版本是1.8.2,其SDK的開(kāi)發(fā)除了正常的功能完善和擴(kuò)展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,874評(píng)論 10 16
  • 本文是翻譯的 APNs 的官方說(shuō)明 自己英文不是太好,花了不少時(shí)間來(lái)翻譯,其實(shí)之前我是看不進(jìn)去的。后來(lái)發(fā)現(xiàn),只要你...
    KyleBing閱讀 2,007評(píng)論 0 0
  • 來(lái)源:崔江濤的博客 概述在多數(shù)移動(dòng)應(yīng)用中任何時(shí)候都只能有一個(gè)應(yīng)用程序處于活躍狀態(tài),如果其他應(yīng)用此刻發(fā)生了一些用戶感...
    李棲桐閱讀 974評(píng)論 0 0
  • 概述 在多數(shù)移動(dòng)應(yīng)用中任何時(shí)候都只能有一個(gè)應(yīng)用程序處于活躍狀態(tài),如果其他應(yīng)用此刻發(fā)生了一些用戶感興趣的那么通過(guò)通知...
    莫離_焱閱讀 6,713評(píng)論 1 8
  • 我們平時(shí)在山上撿柴時(shí),遇到如大腳趾大的、較圓滾的石頭,也如獲至寶地?fù)炱饋?lái),湊夠七顆后,就是一副玩物,用來(lái)在下雨天玩...
    夢(mèng)醉?yè)频逗?/span>閱讀 288評(píng)論 0 0

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