最近极光推送更新到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 集成极光推送的更多相关文章

  1. ios集成极光推送:Undefined symbols for architecture arm64: "_dns_parse_resource_record", referenced from:?

    添加libresolv.tbd库,即可解决问题 Undefined symbols for architecture arm64: "_dns_parse_resource_record&q ...

  2. 李洪强iOS之集成极光推送三iOS集成指南

    李洪强iOS之集成极光推送三iOS集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push S ...

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

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

  4. 李洪强iOS之集成极光推送一iOS SDK概述

    李洪强iOS之集成极光推送一iOS SDK概述 JPush iOS 从上图可以看出,JPush iOS Push 包括 2 个部分,APNs 推送(代理),与 JPush 应用内消息. 红色部分是 A ...

  5. Swift3集成极光推送

      现在很多程序都开始使用Swift开发了,但是第三方库大多数都是用OC写的,所以我们要使用Swift和OC混编.今天的内容主要讲Swift3.0集成极光推送. 1.准备工作   集成指南,极光上说的 ...

  6. C#—ASP.NET:集成极光推送(Push API v3)

    C#—ASP.NET:集成极光推送(Push API v3) 原文地址: https://blog.csdn.net/CXLLLK/article/details/86489994   1.极光推送官 ...

  7. ionic2集成极光推送

    ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...

  8. thinkphp3.2集成极光推送

    项目中用到了给客户端的推送功能,选用了极光推送,下面演示一下在thinkphp中集成极光推送 1.下载极光推送的php类,可以从笔者的git下载 地址:https://git.oschina.net/ ...

  9. Xamarin.Forms学习系列之Android集成极光推送

    一般App都会有消息推送的功能,如果是原生安卓或者IOS集成消息推送很容易,各大推送平台都有相关的Sample,但是关于Xamarin.Forms的消息推送集成的资料非常少,下面就说下Xamarin. ...

随机推荐

  1. python环境和工具

    1.版本问题 python2.X和python3.X是不兼容,所以选择如果选择了2.X版本,那么为了避免兼容性的问题,在以后使用其他python库或者工具时,也需要选择相对应的版本. 下载地址:htt ...

  2. Unity 平台依赖编译

    位置:unity文档-Manual-Scripting-Platform dependent compilation Property: Function: UNITY_EDITOR #define ...

  3. 转: JQuery this和$(this)的区别及获取$(this)子元素对象的方法

    1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? 首先来看看JQuery中的  $()  这 ...

  4. delphi 实现两个exe文件共享内存映像的代码

    创建内存映像的程序 ------------------------------------------------------------------------------------------ ...

  5. 认识bash和shell

    各个 shell 的功能都差不多, Linux 默认使用 bash ,所以我们主要学习bash的使用. 1.bash命令格式 命令 [-options] [参数],如:tar  zxvf  demo. ...

  6. swift和OC中frame的小差异

    //1.0 OC中 CGRect .CGPoint.CGSize 的结构如下: struct CGRect { CGPoint origin; CGSize size; }; struct CGPoi ...

  7. Python自动化运维开发实战 一、初识Python

    导语 都忘记是什么时候知道python的了,我是搞linux运维的,早先只是知道搞运维必须会shell,要做一些运维自动化的工作,比如实现一些定时备份数据啊.批量执行某个操作啊.写写监控脚本什么的. ...

  8. gradle 很好用的么

    Gradle 其实是很好用的 2017, Apr 14 by Tesla Ice Zhang Gradle 是一款使用 Kotlin (划掉) Groovy 编写的 JVM 构建工具,其易用性和 Ma ...

  9. linux下进程查找和杀死

    比如杀死进程中叫  abc的进程 1.ps -ef |grep abc|awk '{print $2}' xargs kill -9 2.kill `pidof abc`        `位于tab键 ...

  10. Java的学习01

    记录每天的学习情况.加油. /** * 测试包装类 * @author 小白 * */ public class TestWrappedClass { public static void main( ...