首先是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中

step2 添加靜態(tài)庫(kù)
并在Xcode的Link Binary With Libraries中添加libRCTPushNotification.a

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

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

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)行推送。