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

时间:2021-08-11 08:40:29

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

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

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

具体做法如下:

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

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

1、添加usernotification.framework

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

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

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

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

1、设置jpush的sdk的路径

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

2、开启消息推送功能

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

三、代码修改

1、添加usernotification的头文件

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

2、添加usernotification的启用代码

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

3、添加jpush的适配代码

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

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

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

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

补充:完整的使用极光

1、导入相应头文件

?
1
2
3
4
5
6
#import "jpushservice.h"
#import <adsupport/adsupport.h>
#ifdef nsfoundationversionnumber_ios_9_x_max
// 这里是ios10需要用到的框架
#import <usernotifications/usernotifications.h>
#endif

2、启动极光推送功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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、注册

?
1
2
3
4
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken
{
[jpushservice registerdevicetoken:data];
}

4、注册失败

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

5、接收

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

6、处理通知

6-1、ios10以下版本时

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (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及以上版本时

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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
3
4
5
6
7
8
9
10
11
/// 绑定别名(注意: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中的极光推送消息的适配,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/potato512/article/details/52608846