极光推送小结 - 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:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object: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:launchOptions
appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier: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 清0
application.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:nil
cancelButtonTitle:@"取消"
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 {
//Optional
NSLog(@"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)){
// Required
NSDictionary * 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)){
// Required
NSDictionary * 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 调整,希望对大家有所帮助,也希望大神多多指点共进步!
极光推送小结 - iOS的更多相关文章
- 李洪强iOS之集成极光推送三iOS集成指南
李洪强iOS之集成极光推送三iOS集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push S ...
- 李洪强iOS之集成极光推送二iOS 证书 设置指南
李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...
- 李洪强iOS之集成极光推送一iOS SDK概述
李洪强iOS之集成极光推送一iOS SDK概述 JPush iOS 从上图可以看出,JPush iOS Push 包括 2 个部分,APNs 推送(代理),与 JPush 应用内消息. 红色部分是 A ...
- 极光推送 api ios参数问题
这是首个app项目,推送用的是极光推送jpush 由于用官方文档出现接收多条的问题,在网上找到一套封装好的,非常感觉这位开发者 //推送.指定人error_reporting(E_ALL^E_NOTI ...
- ionic3用极光推送笔记
安卓 环境:ionic3 + 极光 "jpush-phonegap-plugin": "^3.4.3" "cordova-plugin-jcore& ...
- iOS推送(利用极光推送)
本文主要是基于极光推送的SDK封装的一个快速集成极光推送的类的封装(不喜勿喷) (1)首先说一下推送的一些原理: Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指 ...
- 【原】iOS学习之极光推送
一.极光推送工程端 1.下载SDK 极光推送是一个推送消息的第三方,SDK下载:https://www.jpush.cn/common/products 集成压缩包内容:包名为JPush-iOS-SD ...
- (转载)iOS 极光推送SDK 集成指南
iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...
- 关于ios极光推送server端注意的地方
今天试用了极光推送API 用它是因为,大多数人说它的文档是最全的,但是用过之后,发现关于IOS的文档,还是很不够,导致走了一点弯路! 特别是服务端的代码:https://github.com/jpus ...
随机推荐
- .Net Mvc框架知识点
一.实现Controller的依赖注入: 1.自定义继承DefaultControllerFactory 类的控制器工厂类并重写GetControllerInstance方法:(如:InjectCon ...
- winform代码生成器(二)
代码下载 地址 http://pan.baidu.com/s/1nuZjyat 接着说 上文继续说,这次我们要生成主从表. 此方用到了第三方的 控件 DevExpress 的Gridview .大家可 ...
- 从零开始的全栈工程师——html篇1.4
背景与边框 一.背景(backgound) 1.背景颜色:background-color:red;(简写:background:color;) 备注:ie9以下给body设置background-c ...
- IoT Architecture
- Spring MVC 如何加载静态html
在spring mvc的xml文件最后面加上下面这一行<mvc:deault-servlet-handler/>
- maven学习(一)setting.xml配置文件详解
maven环境搭建: 1.官网下载zip包,解压至任意目录(如:E:\wly\apache-maven-3.2.5) 2.环境变量MAVEN_HOME(E:\wly\apache-maven-3.2. ...
- pdf2swf 转换时报错。This file is too complex to render- SWF only supports 65536 shapes at once
在使用swftools转换pdf 到swf的时候报错,有如下说明:if the pdf contains too many images / shapes, pdf2swf will fail wit ...
- Visual Studio 快捷键汇总
常见方法: 强迫智能感知:Ctrl+J.智能感知是Visual Studio最大的亮点之一,选择Visual Studio恐怕不会没有这个原因. 撤销:Ctrl+Z.除非你是天才,那么这个快捷键也是 ...
- MongoDB数据库 备份 还原
MongoDB数据库 1.备份用 mongodump 2.还原用 mongorestore 1.备份 @echo offecho 正在备份MongoDB数据库SET mon ...
- 爬虫入门之爬取策略 XPath与bs4实现(五)
爬虫入门之爬取策略 XPath与bs4实现(五) 在爬虫系统中,待抓取URL队列是很重要的一部分.待抓取URL队列中的URL以什么样的顺序排列也是一个很重要的问题,因为这涉及到先抓取那个页面,后抓取哪 ...