ios集成极光推送的总结

说一下最近项目里遇到的极光推送:

具体步骤:

一:首先需要一个推送的p12 证书:我是传送门(创建推送证书)

但是我在导出aps.证书制作p12的时候,我这边在系统钥匙串访问里面产生了两个名字是一样时间不一样的证书,要区分生产的和测试,否则上传到极光会不通过,

二:登录极光账号(我是登录传送门),创建应用并上传p12证书,APNS证书文件显示 已验证 说明通过,其他则不能正确接通极光推送

三:极光的官网集成文档(我是官网传送门)----->里面创建应用,集成SDK都很详细,或者在下载的sdk包里面有一个 iOS+SDK+Integration+Guide.pdf 里面写的比官网看着清楚些,在AppDelegate代码里做一些app在前台的时候的通知处理,在前台的时候就不通知,只做弹窗提示,我的测试代码如下(我这边没做IDFA功能):

```

#import "AppDelegate.h"

// 引 JPush功能所需头文件

#import "JPUSHService.h"

// iOS10注册APNS所需头文件

#ifdef NSFoundationVersionNumber_iOS_9_x_Max 

#import

#endif

```

// 如果需要使 idfa功能所需要引 的头 件(可选)#import

#import "ViewController.h"

#import "TwoViewController.h"

@interface AppDelegate ()

@end

static NSString *appKey = @"3bd1eb35caec54f9c91a493b";

static NSString *channel = @"App Store";

static BOOL isProduction = FALSE;

@implementation AppDelegate

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

if (launchOptions !=nil) {// 不是空 就是推送点击 否则是图标启动        

NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];       

 if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {    

        // iOS 10 不必走此方法           

      [self reciveNotification:remoteNotification];// 处理推送跳转方法 详见下方       

 }   

 }        

//Required    //notice: 3.0.0及以后版本注册可以这样写,也可以继续 之前的注册 式    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];       

 entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;      [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];       

 /* Optional    获取IDFA    如需使 IDFA功能请添加此代码并在初始化 法的advertisingIdentifier参数中填写对应值    需要导入

#importNSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

Required

init Push

notice: 2.1.5版本的SDK新增的注册 法,改成可上报IDFA,如果没有使 IDFA直接传nil

如需继续使 pushConfig.plist 件声明appKey等配置内容,请依旧使 [JPUSHService setupWithOption:launchOptions] 式初始化。

如不需要使用IDFA,advertisingIdentifier 可为nil

*/

[JPUSHService setupWithOption:launchOptions appKey:appKey

channel:channel

apsForProduction:isProduction

advertisingIdentifier:nil];

[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {

if(resCode == 0){

NSLog(@"registrationID获取成功:%@",registrationID);

}

else{

NSLog(@"registrationID获取失败,code:%d",resCode);

}

}];

//开启Crash日志收集

[JPUSHService crashLogON];

return YES;

}

// Required - 注册 DeviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

[JPUSHService registerDeviceToken:deviceToken];

}

//Optional 注册失败

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

NSLog(@"注册失败: %@", error);

}

//iOS 7 及以上10以下 收到推送及点击处理

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

[JPUSHService handleRemoteNotification:userInfo];

if (application.applicationState == UIApplicationStateActive) {

NSLog(@"userInfo----%@",userInfo);

// 前台收到推送出现弹窗

[self reciveNotificationAlertShow:@"userInfo"];

}else{

// 处于后台 的点击

[self reciveNotification:userInfo];

}

completionHandler(UIBackgroundFetchResultNewData);

}

#pragma mark- JPUSHRegisterDelegate

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#pragma mark iOS 10 前台收到通知(远程推送 和 本地通知)

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {

NSDictionary * userInfo = notification.request.content.userInfo;

if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

// iOS10处理远程推送

[JPUSHService handleRemoteNotification:userInfo];

NSDictionary * userInfo = notification.request.content.userInfo;

UNNotificationRequest *request = notification.request; // 收到推送的请求

UNNotificationContent *content = request.content; // 收到推送的消息内容

//前台接收弹框

NSLog(@"在前台处理本地通知----iOS10 收到远程通知:%@", userInfo);

[self reciveNotificationAlertShow:content.body];

}else{

// iOS10处理本地通知

// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置

completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);

}

}

// 程序运行于后台 或者被杀死 点击推送通知 都会走这个方法

- (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];

NSLog(@"在后台台处理通知----iOS10 收到远程通知:%@", userInfo);

[self reciveNotification:userInfo];

}else{

/// 前台运行时收到推送 转的本地通知,如果没有查看,而是退到后台 或杀死程序,点击了推送到前台push处理==============

/// 前台运行时 转的本地通知 直接点击也走这个方法

[self reciveNotification:userInfo];

}

completionHandler();  // 系统要求执行这个方法

}

#endif

//当程序从后台将要重新回到前台时候调用 把角标的 1 去掉

- (void)applicationWillEnterForeground:(UIApplication *)application {

[application setApplicationIconBadgeNumber:0];

}

// APP杀死和后台时 推送点击的跳转处理

- (void)reciveNotification:(NSDictionary *)pushDict{

NSLog(@" 推送点击的跳转处理----%@",pushDict);

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

TwoViewController *twoVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"TwoViewController"];

twoVC.saveDict = pushDict;

[[self topViewController].navigationController pushViewController:twoVC animated:YES];

}

// APP 处于前台的时候 推送通知点击创建alertview  点击跳转方法和杀死 后台运行状态处理方法相同

- (void)reciveNotificationAlertShow:(NSString *)message{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *goAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

TwoViewController *twoVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"TwoViewController"];

twoVC.backStr = message;

[[self topViewController].navigationController pushViewController:twoVC animated:YES];

}];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

[alertController addAction:goAction];

[alertController addAction:cancelAction];

[[self topViewController] presentViewController:alertController animated:YES completion:nil];

}

#pragma mark 获取当前的停留的VC用来实现任意页面跳转到指定页面

- (UIViewController*)topViewController

{

return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];

}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController

{

if ([rootViewController isKindOfClass:[UITabBarController class]]) {

UITabBarController *tabBarController = (UITabBarController *)rootViewController;

return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];

} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {

UINavigationController* navigationController = (UINavigationController*)rootViewController;

return [self topViewControllerWithRootViewController:navigationController.visibleViewController];

} else if (rootViewController.presentedViewController) {

UIViewController* presentedViewController = rootViewController.presentedViewController;

return [self topViewControllerWithRootViewController:presentedViewController];

} else {

return rootViewController;

}

}

你可能感兴趣的:(ios集成极光推送的总结)