最近极光推送更新到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. java-部分精选面试题

    JVM的类加载机制是什么?有哪些实现方式? 类加载机制: 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法去内,然后在堆区创建一个java.lang.Clas ...

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

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

  3. Window环境下Python和Django的安装,以及项目的创建

    1.首先我们要下载python和Django,他们的下载地址如下 python地址:https://www.python.org/ Django地址:  https://www.djangoproje ...

  4. vscode项目配置 vue-loader-webpack

    使用vsCode进行项目配置 一.准备工作 1.下载Visual Studio Code 下载地址 2.打开vscode,根据自己需求下载相应插件,可以提高工作效率. 点击下角选项(我作了红框标记), ...

  5. KVM虚拟化技术(六)磁盘管理

    KVM支持的虚拟磁盘类型 raw 这并非是一种真正的磁盘格式,而是代表虚拟机所使用的原始镜像:它并不存储元数据,因此可以作为保证虚拟机兼容性的候选方案,然而也正因为 它不存储元数据,因此不能支持某些高 ...

  6. Structs复习 包含外部xml和默认Actiion

    包含外部xml 可以用 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PU ...

  7. mysql 远程 ip访问

    默认情况下Linux内的mysql数据库mysql,user表内的用户权限只是对localhost即本机才能登陆.需要更改权限: 如下的方式确认: root#mysql -h localhost-u  ...

  8. quartz整合spring框架service层对象注入为null解决方案

    Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...

  9. tomcat 启动报错org.hibernate.cfg.annotations.SimpleValueBinder.setType

    url: https://blog.csdn.net/zhx_0323/article/details/78844323 # A fatal error has been detected by th ...

  10. day18 logging模块 sys shelve

    昨日回顾 re 正则表达式 匹配字符串 场景 例如:爬虫,密码规则验证,邮箱地址验证,手机号码 学习re主要学习的就是 那一堆特殊符号 hashlib hash是一种算法 lib表示库 该模块包含了一 ...