iOS 集成极光推送
最近极光推送更新到V3版本之后,推送又不成功!配合服务器联调了半天,发现是服务器环境配置有问题。
想着就把极光推送的步骤给记录下来。
一、配置push证书
这个可以到极光文档里写,很详细
二、导入必要的框架
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib
三、代码中注册极光推送
首先在AppDelegate.m 导入#import "JPUSHService.h"
在~didFinishLaunchingWithOptions方法贴上核心代码:
// 1.注册系统通知
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:settings];
}
// 2.注册极光推送
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:@"注册极光生成的appKey"
channel:@"Publish channel"
apsForProduction:NO
advertisingIdentifier:nil]; // 接收应用内消息
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
// 极光推送登录成功后可以注册别名
[defaultCenter addObserver:self selector:@selector(registerAlias:) name:kJPFNetworkDidLoginNotification object:nil];
// 3.注册 DeviceToken
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
四、接收通知
#pragma mark - JPUSHRegisterDelegate
// iOS 10 Support,前台收到通知,后台不会执行这个方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
// 通知内容为:notification.request.content.body
} // iOS 10 Support,用户点击了通知进入app
- (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];
}
completionHandler(); // 系统要求执行这个方法
}
#pragma mark 解析极光推送的应用内消息
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSLog(@"解析极光推送的应用内消息:%@",userInfo);
} // 接收到远程通知之后
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//获取 deviceToken 失败后 远程推送(极光推送)打开失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"获取 device token 失败 %@", error);
}
五、注册别名(可选)
主要是在项目的登录成功或者自动登录后,使用用户的唯一标示进行绑定
// 注册别名
[JPUSHService setAlias:@"一般为用户的账号" callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self]; // 极光别名注册的回调方法
-(void)tagsAliasCallback:(int)iResCode
tags:(NSSet*)tags
alias:(NSString*)alias
{
NSLog(@"极光别名注册的回调方法rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
if (iResCode == ) {
// 注册成功
}
}
去除绑定
用户进行退出登录的方法里添加去除绑定即可,值得注意的是用到即时通讯的话,被挤下线也要去除绑定,
//没有值就代表去除
[JPUSHService setTags:[NSSet set]callbackSelector:nil object:self];
[JPUSHService setAlias:@"" callbackSelector:nil object:self];
[JPUSHService setTags:[NSSet set] alias:@"" callbackSelector:nil target:self];
iOS 集成极光推送的更多相关文章
- ios集成极光推送:Undefined symbols for architecture arm64: "_dns_parse_resource_record", referenced from:?
添加libresolv.tbd库,即可解决问题 Undefined symbols for architecture arm64: "_dns_parse_resource_record&q ...
- 李洪强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 ...
- Swift3集成极光推送
现在很多程序都开始使用Swift开发了,但是第三方库大多数都是用OC写的,所以我们要使用Swift和OC混编.今天的内容主要讲Swift3.0集成极光推送. 1.准备工作 集成指南,极光上说的 ...
- C#—ASP.NET:集成极光推送(Push API v3)
C#—ASP.NET:集成极光推送(Push API v3) 原文地址: https://blog.csdn.net/CXLLLK/article/details/86489994 1.极光推送官 ...
- ionic2集成极光推送
ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...
- thinkphp3.2集成极光推送
项目中用到了给客户端的推送功能,选用了极光推送,下面演示一下在thinkphp中集成极光推送 1.下载极光推送的php类,可以从笔者的git下载 地址:https://git.oschina.net/ ...
- Xamarin.Forms学习系列之Android集成极光推送
一般App都会有消息推送的功能,如果是原生安卓或者IOS集成消息推送很容易,各大推送平台都有相关的Sample,但是关于Xamarin.Forms的消息推送集成的资料非常少,下面就说下Xamarin. ...
随机推荐
- easy.py使用中ValueError: could not convert string to float: svm_options错误问题解决
在使用easy.py中出现如下图所示问题 解决方法: 1.找到cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{ ...
- git 合并多个commit
1,查看提交历史,git log 首先你要知道自己想合并的是哪几个提交,可以使用git log命令来查看提交历史,假如最近4条历史如下: commit 3ca6ec340edc66df13423f36 ...
- python小实例一:简单爬虫
本文所谓的爬虫就是通过本地远程访问url,然后将url的读成源代码形式,然后对源代码进行解析,获取自己需要的数据,相当于简单数据挖掘.本文实现的是将一个网页的图片爬出保存到本地的过程,例子很简单,用的 ...
- 21.struts-Action配置.md
目录 1.Action开发方式 2.通配符 访问地址 [toc] 3.常量 后缀 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freema ...
- Hibernate学习笔记3.1(Hibernate关系映射)
主要指对象之间的关系 1.一对一关联 一对一单项外键关联 比如说一夫一妻 Wifi.java package com.bjsxt.hibernate; import javax.persistence ...
- highstock无图像
如果你的x轴是时间又是世纪秒的话又按以下设置的话,把xAxis的设置去掉试试看, 因为highstock会对世纪秒自动转换的 // xAxis: {// // max: 23, // min: 0, ...
- 吴裕雄 python 熵权法确定特征权重
一.熵权法介绍 熵最先由申农引入信息论,目前已经在工程技术.社会经济等领域得到了非常广泛的应用. 熵权法的基本思路是根据各个特征和它对应的值的变异性的大小来确定客观权重. 一般来说,若某个特征的信息熵 ...
- HTTP是用来做什么的
(一)HTTP协议介绍 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目 ...
- shiro 基于springmvc中做登陆功能
1.添加依赖 <!-- shiro --> <dependency> <groupId>org.apache.shiro</groupId> <a ...
- 03_java基础(七)之面向对象
16.封装查询结果对象 封装简单粗暴的理解就是:假设你在超市买苹果,买一个你可以一个手拿走,买两个你可以用两只手拿走,但是如果买了20个勒,咋办勒,那就用一个袋子装起来!这就 封装思想. 1.封装一个 ...