React-Native-iOS推送集成
来源:互联网 发布:数据缺失的解决方案 编辑:程序博客网 时间:2024/06/05 07:02
首先是iOS推送流程,这篇文章有了比较详细的描述,大家可以了解一下http://www.jianshu.com/p/54ba62b2ed77
iOS推送流程
而本文主要是针对的需求是在已拥有自己的推送服务器的情况下,怎么在js
端获取已注册的device token
,并将其交由后台去保存。今后的推送流程就是,当有消息需要推送时,后台调用推送服务器相应接口传递消息,推送服务器将消息推给APNS
(苹果推送服务器)。APNS再通过注册的device token
推给相应手机。
如果想直接集成三方推送平台的话,可参考以下文章
React-Native-iOS极光推送
1.前置准备
- iOS推送证书及App相应描述文件
在拥有Apple开发者账号的情况下,可前往苹果开发者网站进行申请,具体流程可见以下优秀文章:
手把手教学iOS推送证书申请
2.导入相应库
1.首先手动链接PushNotificationIOS的库:
step1 添加项目依赖
将node_modules/react-native/Libraries/PushNotificationIOS/RCTPushNotification.xcodeproj
文件拖到Xcode界面的Library中
step2 添加静态库
并在Xcode的Link Binary With Libraries
中添加libRCTPushNotification.a
2.打开推送的相关配置
1.开启推送功能
2.开启后台推送
3.替换项目文件
更改项目中的AppDelegate.m文件
step1 引入依赖库、设置代理
#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
中加入以下代码(目的:根据系统版本注册deviceToken
)
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { //iOS10特有 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // 必须写代理,不然无法监听通知的接收与点击 center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { // 点击允许 NSLog(@"注册成功"); [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"%@", settings); }]; } else { // 点击不允许 NSLog(@"注册失败"); } }]; }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系统以下 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } // 注册获得device Token [[UIApplication sharedApplication] registerForRemoteNotifications];
在上个方法后添加以下方法
// 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中获取推送相应参数
1.导入PushNotificationIOS
import PushNotificationIOS from 'react-native';
2.添加相应监听事件
//界面加载完成时 注册监听事件 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); } //界面即将消失时 注销监听事件 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.将设备device token 交由后台处理
这个可能要具体问题具体分析,根据相应业务需求.
但device token 主要用途是当Provider(本地推送服务器)
需要推送消息给APNS
(苹果推送服务器)时,传递相应的device token
.APNS
找到设备编号,进行推送。
- React-Native-iOS推送集成
- react-native 集成极光推送
- React Native Android JPush消息推送集成
- ios原生集成React Native
- ios集成react-native步骤
- 【react-native-0.31-iOS】iOS集成react-native(01)
- iOS React Native 混合开发集成React Native
- iOS原生项目中集成React Native
- iOS原生项目中集成React Native
- iOS原生项目中集成React Native
- React Native集成到原生项目(IOS)
- React Native 集成到原生项目(iOS)
- iOS原生项目中集成React Native
- React Native 集成到原生项目(iOS)
- React Native集成到IOS原生项目
- React-Native 与IOS集成交互
- React Native集成到IOS应用
- 最新iOS原生项目集成React-Native
- 完美的一对------数据分析和市场营销
- R语言学习-JOIN的各种实现
- Apache 搭建一个图片服务器
- python_淘宝
- EXEM技术干活
- React-Native-iOS推送集成
- 取余是什么?
- PHP 源码学习之线程安全
- 一些诗作-2015年
- 查看kettle两个执行器pan和kitchen的参数信息
- JAVA数据结构之线性表的顺序存储
- js深拷贝对象
- Spring2.5 IoC之bean的四种注入方式(实践篇)
- jQuery Validate验证框架详解