React-Native-iOS推送集成

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


Snip20170704_1.png

step2 添加静态库

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


Snip20170704_2.png

2.打开推送的相关配置

1.开启推送功能


Snip20170704_4.png

2.开启后台推送


Snip20170704_5.png

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找到设备编号,进行推送。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值