最近极光推送更新到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];

最新文章

  1. Centos7下安装python,查看python版本
  2. 使用Github Pages建独立博客
  3. Shell脚本中执行mysql的几种方式(转)
  4. sql 联合查询并更新
  5. android.graphics.Color
  6. C++ Primer 学习笔记_2_高速入口(继续)
  7. design pattern Builder 生成器设计模式
  8. 上锁 - leetcode
  9. 使用httperrequest,模拟发送及接收Json请求
  10. 实战3--项目开始--准备:::资源分类, 日志系统, 写BaseDao
  11. 在vue-cli项目中使用echarts
  12. 利用CSS3制作网页动画
  13. Python3中无法导入ssl模块的解决办法
  14. Windows提权与开启远程连接
  15. BeanUtils.copyProperties的简单示例
  16. Unity ---WidgetsUI CreateTileView Demo
  17. oracle 之 伪列 rownum 和 rowid的用法与区别
  18. 201621123001 《Java程序设计》第13周学习总结
  19. POJ3259 :Wormholes(SPFA判负环)
  20. hdu-1034(模拟+小朋友分糖)

热门文章

  1. CSRF学习小结
  2. A New Year, A New Accent!
  3. week6 10 后端backend server和mongoDB通信
  4. Hibernate学习笔记2.2(Hibernate基础Annotation配置)
  5. spark LBFGS 设置参数
  6. ADO.Net 数据库查询
  7. 微信小程序---模版
  8. java-学习5
  9. js判断是否为undefined
  10. yyyy-MM-dd 转换为年月日