此次即友盟分享小结(友盟分享小结 - iOS)之后对推送也进行了一版优化.此次分享内容依然基于已经成功集成 SDK 后 code 层级部分.
注:此次分享基于 SDK 3.1.0,若版本相差较大,仅供参考.

极光推送官方文档: https://docs.jiguang.cn/jpush/guideline/intro/

首先,为分享单独创建了一个类,为了可以更加清晰的划分其内容部分.

注:创建该子类后,切记将其头文件引入到 AppDelegate 类中.

#import "AppDelegate.h"
// Push
#import "AppDelegate+JPush.h"

  

其次,将项目工程中配置相关配置.

最后,便是具体 code 相关内容,将申请的相关 key 预先设置成宏准备好,方便使用和变更时进行调用和更改.

一.声明

优先将初始化的相关接口配置声明准备好.

#import "AppDelegate.h"@interface AppDelegate (JPush)/**JPush 注册@param launchOptions 启动项*/
- (void)registerJPush:(NSDictionary *)launchOptions;@end

将子类中声明的方法在 AppDelegate 的方法中调用.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// UserDefaults 相关[[DZSBUserInfo sharedInstance] loadCacheData];// CoreData 相关[[CoreDataManager sharedCoreDataManager] managedObjectContext];// 友盟相关[self umAssociatedDetailSettings];// Push[self registerJPush:launchOptions];// Root VC[self setRootViewController];return YES;
}

  

二.实现

1.引入所需的头文件
2.实现声明类中接口
3.实现具体方法和代理事件

#import "AppDelegate+JPush.h"
#import <JPUSHService.h>
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import <AdSupport/AdSupport.h>static NSString *appKey = JPush_APPKEY;
static NSString *channel = JPush_CHANNEL;// @"AppStore"
static BOOL isProduction = FALSE;@interface AppDelegate () <JPUSHRegisterDelegate>@end@implementation AppDelegate (JPush)- (void)registerJPush:(NSDictionary *)launchOptions {//Required//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {// 可以添加自定义categories// NSSet<UNNotificationCategory *> *categories for iOS10 or later// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9}[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];// 注册// apn 内容获取:
//    NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];// 监听自定义消息[kNotificationCenter addObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];// Optional// 获取IDFA// 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];// Required// init Push// notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
//    [JPUSHService setupWithOption:launchOptions
//                           appKey:appKey
//                          channel:channel
//                 apsForProduction:isProduction];[JPUSHService setupWithOption:launchOptionsappKey:appKeychannel:channelapsForProduction:isProductionadvertisingIdentifier:nil];[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {NSLog(@"JPush --- resCode : %d,registrationID: %@",resCode,registrationID);//        [JPUSHService setDebugMode];[JPUSHService setLogOFF];// 生成环境调用}];
}/**获取自定义消息推送内容content:获取推送的内容messageID:获取推送的messageID(key为@"_j_msgid")extras:获取用户自定义参数customizeField1:根据自定义key获取自定义的value@param notification 结构体*/
- (void)networkDidReceiveMessage:(NSNotification *)notification {/*接收消息样式{content = "\U7529\U9505\U7ed9IOS\Uff01";extras =     {fFunPageUrl = "www.baidu.com";fType = 2;};title = "\U6d4b\U8bd5";}*/NSDictionary * userInfo = [notification userInfo];/** 消息标题*/NSString *content = [userInfo valueForKey:@"content"];/** 消息内容*/NSDictionary *extras = [userInfo valueForKey:@"extras"];NSString *messageID = [userInfo valueForKey:@"_j_msgid"];NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字段,key是自己定义的}#pragma mark - Delegate
/**Required - 注册 DeviceToken注:JPush 3.0.9 之前的版本,必须调用此接口,注册 token 之后才可以登录极光,使用通知和自定义消息功能。从 JPush 3.0.9 版本开始,不调用此方法也可以登录极光。但是不能使用APNs通知功能,只可以使用JPush自定义消息。@param application 应用@param deviceToken 标识*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {[JPUSHService registerDeviceToken:deviceToken];
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {// Required,For systems with less than or equal to iOS6// 取得 APNs 标准信息内容NSDictionary *aps = [userInfo valueForKey:@"aps"];NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量NSString *sound = [aps valueForKey:@"sound"]; //播放的声音// 取得Extras字段内容NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服务端中Extras字段,key是自己定义的NSLog(@"JPush\ncontent =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content,(long)badge,sound,customizeField1);[JPUSHService handleRemoteNotification:userInfo];NSLog(@"JPush - Receive notice\n%@", userInfo);// iOS badge 清0application.applicationIconBadgeNumber = 0;
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {// Required, iOS 7 Support[JPUSHService handleRemoteNotification:userInfo];// 应用正处理前台状态下,不会收到推送消息,因此在此处需要额外处理一下if (application.applicationState == UIApplicationStateActive) {UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"收到推送消息"message:userInfo[@"aps"][@"alert"]delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];[alert show];}NSLog(@"JPush - Receive notice\n%@", userInfo);// 收到通知处理相关事项 contentType(系统消息 & 系统公告NSString *contentType = [NSString stringWithFormat:@"%@", [userInfo objectForKey:@"contentType"]];// 消息集合NSMutableDictionary *dicMessage = [[NSMutableDictionary alloc] init];if ([contentType isEqualToString:@""]) {//系统消息// do somethings} else if ([contentType isEqualToString:@""]){//系统公告// do somethings}// block 回调completionHandler(UIBackgroundFetchResultNewData);
}/**注册 APNs 失败@param application 应用@param error       异常*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error {//OptionalNSLog(@"JPush --- did Fail To Register For Remote Notifications With Error: %@\nLocalizedDescription: %@", error, error.localizedDescription);
}#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler  API_AVAILABLE(ios(10.0)){// RequiredNSDictionary * userInfo = notification.request.content.userInfo;if (@available(iOS 10.0, *)) {if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];}} else {// Fallback on earlier versions}if (@available(iOS 10.0, *)) {completionHandler(UNNotificationPresentationOptionAlert);// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置} else {// Fallback on earlier versions}
}// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)){// RequiredNSDictionary * userInfo = response.notification.request.content.userInfo;if (@available(iOS 10.0, *)) {if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];}} else {// Fallback on earlier versions}completionHandler();  // 系统要求执行这个方法
}@end

注:以上实现部分所分享的内容是针对集成后的基本对接部分,其中不包含接收消息后的具体业务逻辑,具体业务逻辑需要根据产品需求在代理回调部分进行单独自行定制开发.

  

分享内容中可能存在的缩写内容部分 code 如下:

#pragma mark - 缩写
#define kApplication        [UIApplication sharedApplication]
#define kKeyWindow          [UIApplication sharedApplication].keyWindow
#define kAppDelegate        ((AppDelegate*)[UIApplication sharedApplication].delegate)
#define kUserDefaults       [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]

  

以上便是此次分享的全部内容,较为简易的推送小结,具体还以实际需求为准,可以自行 diy 调整,希望对大家有所帮助,也希望大神多多指点共进步!

转载于:https://www.cnblogs.com/survivorsfyh/p/9718602.html

极光推送小结 - iOS相关推荐

  1. 李洪强iOS之集成极光推送二iOS 证书 设置指南

    李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...

  2. 极光推送 api ios参数问题

    这是首个app项目,推送用的是极光推送jpush 由于用官方文档出现接收多条的问题,在网上找到一套封装好的,非常感觉这位开发者 //推送.指定人error_reporting(E_ALL^E_NOTI ...

  3. 极光推送使用 IOS端

    极光推送官网:https://www.jpush.cn 极光推送IOS官方文档:http://docs.jpush.cn/display/dev/iOS 集成方式 1.在JPush Portal上创建 ...

  4. android极光推送声音,【极光推送】iOS APNS 自定义铃声

    IOS APNS自定义推送铃声 是推送给苹果服务器时 将 推送的 key = sound 的value = @"custom.xxx".自定义铃声支持几种固定的格式.本人只以.ca ...

  5. 极光推送之iOS系统---devicetoken

    iOS系统的推送比Android系统会稍微复杂一点,此贴就说一些本人在开发过程中遇到的问题. 开始之前,要确保系统证书是有推送权限-APNS 1.deviceToken的获取问题 ios系统要成功将推 ...

  6. 最新极光推送在ios模拟器上无法运行

    保证文档上的Framework全部添加 在build setting中找到build active architecture only ,将其中的debug 设置为YES: 这个属性设置为yes,是为 ...

  7. Android集成极光推送踩坑(二)升级篇

    转载请标明出处 http://blog.csdn.net/mohan6/article/details/74133186 本文作者:[默寒的博客] 前言 前段时间针对集成极光推送写了篇文章( Andr ...

  8. iOS:极光推送控制器跳转

    在前面已经做完了极光消息的推送,那么有消息了,如何跳转到需要的控制器呢?其实,主要还是在userInfo这个消息里面做判断来处理,具体如下: 下面这两个是远程推送时接收消息的方法,这是应用程序提供的方 ...

  9. java激光推送ios_关于ios极光推送server端注意的地方

    今天试用了极光推送API 用它是因为,大多数人说它的文档是最全的,但是用过之后,发现关于IOS的文档,还是很不够,导致走了一点弯路! 特别是服务端的代码:https://github.com/jpus ...

最新文章

  1. STL vector list deque区别与实现
  2. 前端资源整理 - 订阅、工具等
  3. IDEA基于kotlin开发android程序配置小结
  4. 【Python基础知识-pycharm版】第五节-字典\集合
  5. 用深度强化学习玩atari游戏_(一)深度强化学习·入门从游戏开始
  6. oracle建表6大约束,oracle 建表 约束 constraint
  7. 在使用pydelicious时出现HTTP Error 500: Internal Server Error的错误的解决方法:
  8. 让django完成翻译,迁移数据库模型
  9. JavaWeb知识点
  10. 2022显卡、CPU天梯图
  11. 怎样在计算机查汉字,推荐一种集汉字识字、查字、计算机输入于一体的规范汉字字形输入法—郑码(之二)...
  12. FileUpload文件上传控件
  13. jquery版本安全漏洞问题
  14. 干货分享:免费文字转语音工具哪个比较好?
  15. Qt 之 QQ系统表情—实现动态显示效果
  16. CrowdPose: Efficient Crowded Scenes Pose Estimation and A New Benchmark
  17. 一个合格的程序员所具备的素质和修养
  18. CodeForces-1016C Vasya And The Mushrooms(模拟+思维+前缀和的前缀和) 解题报告 Apare_xzc
  19. 一本通 1335:【例2-4】连通块
  20. 面包菜单收起和出现案例

热门文章

  1. C语言 数组指针 - C语言零基础入门教程
  2. Python basestring函数- Python零基础入门教程
  3. android paint 线宽_android Paint 设置线宽setStrokeWidth()的单位
  4. android 添加随意拖动的桌面悬浮窗口,android 添加随意拖动的桌面悬浮窗口
  5. python查看系统句柄数量_linux下查看系统进程占用的句柄数方法
  6. html中basefont标签,HTML的basefont标签
  7. linux关闭自检测进程,CentOS下自动发邮件检测某进程是否存在
  8. ubuntu下android源码编译环境,ubuntu12.04 64位上搭建android源码编译环境
  9. matlab中的uint8函数,未定义与 'uint8' 类型的输入参数相对应的函数 'fitnessty'
  10. php中手机端ajax上拉加载更多,jQuery手机网页上拉加载更多