极光推送小结 - iOS

此次即友盟分享小结(友盟分享小结 - 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/537793.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

word去除所有的空行

申请软著时&#xff0c;需要复制源代码到word里。每行代码不能有换行&#xff0c;要紧凑的80页代码。每页要50~55行代码。 字体可设置为&#xff1a;宋体&#xff0c;5号&#xff0c;行间距固定值12。 演示实例 去除下面word代码里的空行 第一步&#xff1a; word显示隐藏的…

c语言Wndproc未定义,为什么我的老是未定义

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼这是源代码#includeLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow){static TCHAR szAppName[] TEXT("HelloWin&qu…

spark on yarn

2019独角兽企业重金招聘Python工程师标准>>> spark on yarn 软件安装 当前环境 hadoop环境搭建参考&#xff1a;hadoop集群安装 hadoop2.6spark-2.2.0-bin-hadoop2.6.tgzscala-2.11.12安装scala tar -zxvf scala-2.11.12.tgz vi /etc/profile 添加以下内容 export S…

如何查看SQL Server2000执行过的SQL语句

SQLServer事件探查器可以完整记录SQL服务器执行过的SQL语句以及存储过程等 下面是SQLServer事件探查器的使用方法&#xff1a; 1. 打开SQL Server 企业管理器。 2. 从“工具”菜单选择“事件探查器”。 3. 当“事件探查器”主界面打开后&#xff0c;从“文件”菜单选择“新跟踪…

c语言链表不带头节点的排序,不带头结点的单向链表排序——插入排序(C语言)...

LinkList* LinkListInsertSort(LinkList* pHead){LinkList *pFirst (LinkList *)NULL; /* 原链表剩下未排序节点的头指针 */LinkList *pCurrInsert (LinkList *)NULL; /* 无序链表中当前待插入节点 */LinkList *pPrev (LinkList *)NULL; /* 有序链表中插入位置的前一个节点 …

免费字体

若不想字体版权有问题&#xff0c;可以参考以下几种完全免费的字体&#xff1a; 方正&#xff1a;方正黑体、方正书宋、方正仿宋、方正楷体 思源&#xff1a;思源黑体、思源柔黑体、思源宋体 站酷&#xff1a;站酷酷黑体、站酷高端黑体、站酷快乐体、站酷意大利体 。

【Python爬虫学习笔记12】Ajax数据爬取简介

有时候在我们设计利用requests抓取网页数据的时候&#xff0c;会发现所获得的结果可能与浏览器显示给我们的不一样&#xff1a;比如说有的信息我们通过浏览器可以显示&#xff0c;但一旦用requests却得不到想要的结果。这种现象是因为我们通过requests获得的都是HTML源文档&…

c语言的报告一,C语言实验报告(一).doc

C语言实验报告(一)C语言实验报告(一)一、实验目的掌握C语言中&#xff0c;基本的输入输出函数的使用方法。掌握printf中转义字符’\t’&#xff0c;’\n’的用法。掌握赋值语句的用法。掌握算术表达式、赋值表达式的计算。掌握数学函数的使用。二、实验内容从键盘输入一个6位整…

数据挖掘——相似文章推荐

相似文章推荐&#xff1a;在用户阅读某篇文章时&#xff0c;为用户推荐更多的与在读文章内容相类似的文章 相关概念&#xff1a; 推荐(Recommended)&#xff1a;指介绍好的人或事物&#xff0c;希望被任用或接受。数据挖掘领域&#xff0c;推荐包括相似推荐和协同过滤推荐。 相…

win10投影无法正常使用:我们正在确认此功能 解决方法

鼠标移动到开始按钮&#xff0c;右键----- windows powershell&#xff08;管理员&#xff09; 输入命令&#xff1a; netsh winsock reset 然后重启电脑&#xff0c;问题解决

青海师大c语言研究生专业课,2016年青海师范大学计算机应用技术C语言程序设计考研复试题库...

一、选择题1&#xff0e; 有如下程序:程序运行后的输出结果是( )。答:C【解析】题目中判断if 条件是否成立&#xff0c;后a 自增 得if 条件不成立执行else 函数体&#xff0c;输出F 。最后执行语句故C 选项正确。 按照格式输出2&#xff0e; 有如下程序&#xff1a;先取值为0, …

产品经理和项目经理的差别

原文地址&#xff1a;https://blog.csdn.net/verifocus/article/details/79219539 --------------------------------------------------------------------- 项目经理与产品经理的区别&#xff0c;用一句话概括就是&#xff1a;产品经理是做正确的事情&#xff0c;项目经理是…

c语言设计一个按时间片轮转法实现处理器调度的程序,设计一个按时间片轮转法实现处理器调度的程序...

实验一处理器调度一、实习内容选择一个调度算法&#xff0c;实现处理器调度。&#xff1b;二、实习目的在采用多道程序设计的系统中&#xff0c;往往有若干个进程同时处于就绪状态。当就绪进程个数大于处理器数时&#xff0c;就必须依照某种策略来决定哪些进程优先占用处理器。…

Perl的浅拷贝和深度拷贝

首先是深、浅拷贝的概念&#xff1a; 浅拷贝&#xff1a;shallow copy&#xff0c;只拷贝第一层的数据。Perl中赋值操作就是浅拷贝深拷贝&#xff1a;deep copy&#xff0c;递归拷贝所有层次的数据&#xff0c;Perl中Clone模块的clone方法&#xff0c;以及Storable的dclone()函…

程序员分析报告(2018)-总结篇

一、生活中的程序员 居住篇 在主要职业群体中&#xff0c;程序员更倾向于租房&#xff0c;20.9%的受访程序员目前自己有房&#xff0c;此比例明显低于其他职业。大概是因为程序员大部分还比较 年轻&#xff0c;传说中的超高薪水并不能让很多人拥有自己的房子而更长的工作…

linux lvm 查看,Linux LVM 详解

逻辑卷管理LVM是一个多才多艺的硬盘系统工具。无论在Linux或者其他类似的系统&#xff0c;都是非常的好用。传统分区使用固定大小分区&#xff0c;重新调整大小十分麻烦。但是&#xff0c;LVM可以创建和管理“逻辑”卷&#xff0c;而不是直接使用物理硬盘。可以让管理员弹性的管…

cnblogs修改网站图标icon

以下修改网络地址即可 <script type"text/javascript" language"javascript">//Setting ico for cnblogsvar linkObject document.createElement(link);linkObject.rel "shortcut icon";linkObject.href "icon的网络地址";do…

maven settings.xml国内仓库配置

<?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/SETTINGS/1.…

智慧园区-楼宇建模技巧之【建楼篇】

一、国际惯例先上图 二、有什么用&#xff1f;用什么搞的&#xff1f;花了多久&#xff1f; 用途 室内定位(会议室、停车位查找等)安防监控(直接定位到某个楼道的摄像头拉取视频流)各种传感器数据三维可视化请问哪里可以买到呢(含笑半步癫2333) 我这里正好有一个。https://iot.…

android meta工具,android ota 升级包制作分析 (5 工具)

工具1 mkbootfsmkbootfs的源代码在system/core/cpio中。??mkbootfs -f boot_filesystem_config.txt targetfiles/BOOT/RAMDISK | minigzip > ramdisk.img2 mkbootimgmkbootimg的源代码在system/core/mkbootimg中。mkbootimg --kernel kernel --ramdisk ramdisk.img --outp…