当前位置: 移动技术网 > IT编程>移动开发>IOS > 解析iOS10中的极光推送消息的适配

解析iOS10中的极光推送消息的适配

2019年07月24日  | 移动技术网IT编程  | 我要评论

眼前发黑,神经官能症吃什么药,拘捕by风弄

ios10发布后,发现项目中的极光推送接收消息异常了。

查了相关资料后才发现,ios10中对于通知做了不少改变。同时也发现极光也很快更新了对应的sdk。

现在就把适配修改的做法分享一下,希望对有需要的童鞋有所帮助。

具体做法如下:

注意:必须先安装xcode8.0版本。

一、添加相关的skd,或framework文件

1、添加usernotification.framework

2、更新jpush的sdk(最新版本:jpush-ios-2.1.9.a)

二、进行路径和消息推送的配置

1、设置jpush的sdk的路径

2、开启消息推送功能

三、代码修改

1、添加usernotification的头文件

2、添加usernotification的启用代码

3、添加jpush的适配代码

4、添加jpush的代理和代理方法(注意:在appdelegate.m文件中使用)


补充:完整的使用极光

1、导入相应头文件

#import "jpushservice.h" 
#import <adsupport/adsupport.h> 
#ifdef nsfoundationversionnumber_ios_9_x_max 
// 这里是ios10需要用到的框架 
#import <usernotifications/usernotifications.h> 
#endif

2、启动极光推送功能

static nsstring *jpushappkey = @"6abc87b33b23d35b9c3b86e0"; 
static nsstring *jpushchannel = @"publish channel"; 
// static bool jpushisproduction = no; 
#ifdef debug 
// 开发 极光false为开发环境 
static bool const jpushisproduction = false; 
#else 
// 生产 极光true为生产环境 
static bool const jpushisproduction = true; 
#endif 
[objc] view plain copy 在code上查看代码片派生到我的代码片
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { 
// override point for customization after application launch. 
// 启动极光推送 
// required 
// - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { } 
if ([[uidevice currentdevice].systemversion floatvalue] >= 10.0) // ios10 
{ 
#ifdef nsfoundationversionnumber_ios_9_x_max 
jpushregisterentity *entity = [[jpushregisterentity alloc] init]; 
entity.types = (unauthorizationoptionalert | unauthorizationoptionbadge | unauthorizationoptionsound); 
[jpushservice registerforremotenotificationconfig:entity delegate:target]; 
#endif 
} 
else if ([[uidevice currentdevice].systemversion floatvalue] >= 8.0) 
{ 
// categories 
[jpushservice registerforremotenotificationtypes:(uiusernotificationtypebadge | uiusernotificationtypesound | uiusernotificationtypealert) 
categories:nil]; 
} 
else 
{ 
// categories nil 
[jpushservice registerforremotenotificationtypes:(uiremotenotificationtypebadge | uiremotenotificationtypesound | uiremotenotificationtypealert) 
categories:nil]; 
} 
// required 
// [jpushservice setupwithoption:launchoptions] 
// pushconfig.plist appkey 
// 有广告符标识idfa(尽量不用,避免上架审核被拒) 
/* 
nsstring *jpushadvertisingid = [[[asidentifiermanager sharedmanager] advertisingidentifier] uuidstring]; 
[jpushservice setupwithoption:jpushoptions 
appkey:jpushappkey 
channel:jpushchannel 
apsforproduction:jpushisproduction 
advertisingidentifier:jpushadvertisingid]; 
*/ 
// 或无广告符标识idfa(尽量不用,避免上架审核被拒) 
[jpushservice setupwithoption:options 
appkey:jpushappkey 
channel:jpushchannel 
apsforproduction:jpushisproduction]; 
// 2.1.9版本新增获取registration id block接口。 
[jpushservice registrationidcompletionhandler:^(int rescode, nsstring *registrationid) { 
if(rescode == 0) 
{ 
// ios10获取registrationid放到这里了, 可以存到缓存里, 用来标识用户单独发送推送 
nslog(@"registrationid获取成功:%@",registrationid); 
[[nsuserdefaults standarduserdefaults] setobject:registrationid forkey:@"registrationid"]; 
[[nsuserdefaults standarduserdefaults] synchronize]; 
} 
else 
{ 
nslog(@"registrationid获取失败,code:%d",rescode); 
} 
}]; 
return yes; 
}

3、注册

- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken 
{ 
[jpushservice registerdevicetoken:data]; 
}

4、注册失败

- (void)application:(uiapplication *)application didfailtoregisterforremotenotificwationswitherror:(nserror *)error 
{ 
nslog(@"did fail to register for remote notifications with error: %@", error); 
}

5、接收

- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo 
{ 
// apn 内容获取: 
// 取得 apns 标准信息内容 
[jpushservice handleremotenotification:dict]; 
}

6、处理通知

6-1、ios10以下版本时

- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo fetchcompletionhandler:(void (^)(uibackgroundfetchresult))completionhandler 
{ 
dlog(@"2-1 didreceiveremotenotification remotenotification = %@", userinfo); 
// apn 内容获取: 
[jpushservice handleremotenotification:dict]; 
completionhandler(uibackgroundfetchresultnewdata); 
dlog(@"2-2 didreceiveremotenotification remotenotification = %@", userinfo); 
if ([userinfo iskindofclass:[nsdictionary class]]) 
{ 
nsdictionary *dict = userinfo[@"aps"]; 
nsstring *content = dict[@"alert"]; 
dlog(@"content = %@", content); 
} 
if (application.applicationstate == uiapplicationstateactive) 
{ 
// 程序当前正处于前台 
} 
else if (application.applicationstate == uiapplicationstateinactive) 
{ 
// 程序处于后台 
} 
}

6-2、ios10及以上版本时

#pragma mark - ios10: 收到推送消息调用(ios10是通过delegate实现的回调) 
#pragma mark- jpushregisterdelegate 
#ifdef nsfoundationversionnumber_ios_9_x_max 
// 当程序在前台时, 收到推送弹出的通知 
- (void)jpushnotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(nsinteger))completionhandler 
{ 
nsdictionary *userinfo = notification.request.content.userinfo; 
if ([notification.request.trigger iskindofclass:[unpushnotificationtrigger class]]) 
{ 
[jpushservice handleremotenotification:userinfo]; 
} 
// 需要执行这个方法,选择是否提醒用户,有badge、sound、alert三种类型可以设置 
// completionhandler(unnotificationpresentationoptionbadge | unnotificationpresentationoptionsound | unnotificationpresentationoptionalert); 
} 
// 程序关闭后, 通过点击推送弹出的通知 
- (void)jpushnotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler 
{ 
nsdictionary *userinfo = response.notification.request.content.userinfo; 
if ([response.notification.request.trigger iskindofclass:[unpushnotificationtrigger class]]) 
{ 
[jpushservice handleremotenotification:userinfo]; 
} 
completionhandler(); // 系统要求执行这个方法 
} 
#endif

7、其他注意事项

为了保证用户能正常接收,或有针对性的接收通知,登录成功后(或退出后)需要设置别名、标记。通常都是该逻辑都是写在用户登录app成功之后,或者是用户退出当前登录状态后。

/// 绑定别名(注意:1 登录成功或者自动登录后;2 去除绑定-退出登录后) 
+ (void)jpushtagsandaliasinbackgroundtags:(nsset *)set alias:(nsstring *)name 
{ 
// 标签分组(表示没有值) 
nsset *tags = set; 
// 用户别名(自定义值,nil是表示没有值) 
nsstring *alias = name; 
nslog(@"tags = %@, alias = %@(registrationid = %@)", tags, alias, [self registrationid]); 
// tags、alias均无值时表示去除绑定 
[jpushservice settags:tags aliasinbackground:alias]; 
}

以上所述是小编给大家介绍的解析ios10中的极光推送消息的适配,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网