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 <UserNotifications/Usernofications.h>

#endif

```

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

#import "ViewController.h"

#import "TwoViewController.h"

@interface AppDelegate ()<JPUSHRegisterDelegate>

@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;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容