服务器之家

服务器之家 > 正文

iOS10全新推送功能实现代码

时间:2021-01-28 16:21     来源/作者:u012847940

从ios8.0开始推送功能的实现在不断改变,功能也在不断增加,ios10又出来了一个推送插件的开发(见最后图),废话不多说直接上代码: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#import <usernotifications/usernotifications.h>
 
 
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
 // override point for customization after application launch.
 
 /* app未启动,点击推送消息的情况下 ios10遗弃uiapplicationlaunchoptionslocalnotificationkey,使用代理unusernotificationcenterdelegate方法didreceivenotificationresponse:withcompletionhandler:获取本地推送
 */
// nsdictionary *localuserinfo = launchoptions[uiapplicationlaunchoptionslocalnotificationkey];
// if (localuserinfo) {
// nslog(@"localuserinfo:%@",localuserinfo);
// //app未启动,点击推送消息
// }
 nsdictionary *remoteuserinfo = launchoptions[uiapplicationlaunchoptionsremotenotificationkey];
 if (remoteuserinfo) {
 nslog(@"remoteuserinfo:%@",remoteuserinfo);
 //app未启动,点击推送消息,ios10下还是跟以前一样在此获取
 }
 [self registernotification];
 return yes;
}

注册推送方法的改变:

新增库 #import <usernotifications/usernotifications.h>  推送单列unusernotificationcenter 等api 

?
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
- (void)registernotification{
 /*
 identifier:行为标识符,用于调用代理方法时识别是哪种行为。
 title:行为名称。
 uiusernotificationactivationmode:即行为是否打开app。
 authenticationrequired:是否需要解锁。
 destructive:这个决定按钮显示颜色,yes的话按钮会是红色。
 behavior:点击按钮文字输入,是否弹出键盘
 */
 unnotificationaction *action1 = [unnotificationaction actionwithidentifier:@"action1" title:@"策略1行为1" options:unnotificationactionoptionforeground];
 /*ios9实现方法
 uimutableusernotificationaction * action1 = [[uimutableusernotificationaction alloc] init];
 action1.identifier = @"action1";
 action1.title=@"策略1行为1";
 action1.activationmode = uiusernotificationactivationmodeforeground;
 action1.destructive = yes;
 */
 
 untextinputnotificationaction *action2 = [untextinputnotificationaction actionwithidentifier:@"action2" title:@"策略1行为2" options:unnotificationactionoptiondestructive textinputbuttontitle:@"textinputbuttontitle" textinputplaceholder:@"textinputplaceholder"];
 /*ios9实现方法
 uimutableusernotificationaction * action2 = [[uimutableusernotificationaction alloc] init];
 action2.identifier = @"action2";
 action2.title=@"策略1行为2";
 action2.activationmode = uiusernotificationactivationmodebackground;
 action2.authenticationrequired = no;
 action2.destructive = no;
 action2.behavior = uiusernotificationactionbehaviortextinput;//点击按钮文字输入,是否弹出键盘
 */
 
 unnotificationcategory *category1 = [unnotificationcategory categorywithidentifier:@"category1" actions:@[action2,action1] minimalactions:@[action2,action1] intentidentifiers:@[@"action1",@"action2"] options:unnotificationcategoryoptioncustomdismissaction];
 // uimutableusernotificationcategory * category1 = [[uimutableusernotificationcategory alloc] init];
 // category1.identifier = @"category1";
 // [category1 setactions:@[action2,action1] forcontext:(uiusernotificationactioncontextdefault)];
 
 unnotificationaction *action3 = [unnotificationaction actionwithidentifier:@"action3" title:@"策略2行为1" options:unnotificationactionoptionforeground];
 // uimutableusernotificationaction * action3 = [[uimutableusernotificationaction alloc] init];
 // action3.identifier = @"action3";
 // action3.title=@"策略2行为1";
 // action3.activationmode = uiusernotificationactivationmodeforeground;
 // action3.destructive = yes;
 
 unnotificationaction *action4 = [unnotificationaction actionwithidentifier:@"action4" title:@"策略2行为2" options:unnotificationactionoptionforeground];
 // uimutableusernotificationaction * action4 = [[uimutableusernotificationaction alloc] init];
 // action4.identifier = @"action4";
 // action4.title=@"策略2行为2";
 // action4.activationmode = uiusernotificationactivationmodebackground;
 // action4.authenticationrequired = no;
 // action4.destructive = no;
 
 unnotificationcategory *category2 = [unnotificationcategory categorywithidentifier:@"category2" actions:@[action3,action4] minimalactions:@[action3,action4] intentidentifiers:@[@"action3",@"action4"] options:unnotificationcategoryoptioncustomdismissaction];
 // uimutableusernotificationcategory * category2 = [[uimutableusernotificationcategory alloc] init];
 // category2.identifier = @"category2";
 // [category2 setactions:@[action4,action3] forcontext:(uiusernotificationactioncontextdefault)];
 
 
 [[unusernotificationcenter currentnotificationcenter] setnotificationcategories:[nsset setwithobjects:category1,category2, nil]];
 [[unusernotificationcenter currentnotificationcenter] requestauthorizationwithoptions:unauthorizationoptionbadge | unauthorizationoptionsound | unauthorizationoptionalert completionhandler:^(bool granted, nserror * _nullable error) {
 nslog(@"completionhandler");
 }];
 /*ios9实现方法
 uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound) categories:[nsset setwithobjects: category1,category2, nil]];
 
 [[uiapplication sharedapplication] registerusernotificationsettings:settings];
 */
 [[uiapplication sharedapplication] registerforremotenotifications];
 
 
 [unusernotificationcenter currentnotificationcenter].delegate = self;
}

代理方法的改变:

 一些本地和远程推送的回调放在了同一个代理方法

?
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma mark -
 
- (void)application:(uiapplication *)application didregisterusernotificationsettings:(uiusernotificationsettings *)notificationsettings ns_available_ios(8_0) __tvos_prohibited{
 nslog(@"didregisterusernotificationsettings");
}
 
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken ns_available_ios(3_0){
 nslog(@"devicetoken:%@",devicetoken);
 nsstring *devicetokenst = [[[[devicetoken description]
   stringbyreplacingoccurrencesofstring:@"<" withstring:@""]
  stringbyreplacingoccurrencesofstring:@">" withstring:@""]
  stringbyreplacingoccurrencesofstring:@" " withstring:@""];
 nslog(@"devicetokenst:%@",devicetokenst);
}
 
- (void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error ns_available_ios(3_0){
 nslog(@"didfailtoregisterforremotenotificationswitherror:%@",error);
}
 
/*ios9使用方法
- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo ns_deprecated_ios(3_0, 10_0, "use usernotifications framework's -[unusernotificationcenterdelegate willpresentnotification:withcompletionhandler:] or -[unusernotificationcenterdelegate didreceivenotificationresponse:withcompletionhandler:] for user visible notifications and -[uiapplicationdelegate application:didreceiveremotenotification:fetchcompletionhandler:] for silent remote notifications"){
 
}
*/
 
- (void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler{
 nslog(@"willpresentnotification:%@",notification.request.content.title);
 
 // 这里真实需要处理交互的地方
 // 获取通知所带的数据
 nsstring *notmess = [notification.request.content.userinfo objectforkey:@"aps"];
 
}
 
- (void)usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler{
 //在没有启动本app时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
 nsstring *notmess = [response.notification.request.content.userinfo objectforkey:@"aps"];
 nslog(@"didreceivenotificationresponse:%@",response.notification.request.content.title);
// response.notification.request.identifier
}
 
//远程推送app在前台
- (void)application:(uiapplication *)application didreceiveremotenotification:(nsdictionary *)userinfo fetchcompletionhandler:(void (^)(uibackgroundfetchresult))completionhandler{
 nslog(@"didreceiveremotenotification:%@",userinfo);
}
 
/*
- (void)application:(uiapplication *)application handleactionwithidentifier:(nullable nsstring *)identifier forremotenotification:(nsdictionary *)userinfo completionhandler:(void(^)())completionhandler ns_deprecated_ios(8_0, 10_0, "use usernotifications framework's -[unusernotificationcenterdelegate didreceivenotificationresponse:withcompletionhandler:]") __tvos_prohibited
{
 
}
*/
/*
// 本地通知回调函数,当应用程序在前台时调用
- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification ns_deprecated_ios(4_0, 10_0, "use usernotifications framework's -[unusernotificationcenterdelegate willpresentnotification:withcompletionhandler:] or -[unusernotificationcenterdelegate didreceivenotificationresponse:withcompletionhandler:]") __tvos_prohibited{
 nslog(@"didreceivelocalnotification:%@",notification.userinfo);
 
 
 // 这里真实需要处理交互的地方
 // 获取通知所带的数据
 nsstring *notmess = [notification.userinfo objectforkey:@"aps"];
 uialertview *alert = [[uialertview alloc] initwithtitle:@"本地通知(前台)"
    message:notmess
    delegate:nil
   cancelbuttontitle:@"ok"
   otherbuttontitles:nil];
 [alert show];
 
 // 更新显示的徽章个数
 nsinteger badge = [uiapplication sharedapplication].applicationiconbadgenumber;
 badge--;
 badge = badge >= 0 ? badge : 0;
 [uiapplication sharedapplication].applicationiconbadgenumber = badge;
 
 // 在不需要再推送时,可以取消推送
 [firstviewcontroller cancellocalnotificationwithkey:@"key"];
 
}
 
 
- (void)application:(uiapplication *)application handleactionwithidentifier:(nullable nsstring *)identifier forlocalnotification:(uilocalnotification *)notification completionhandler:(void(^)())completionhandler ns_deprecated_ios(8_0, 10_0, "use usernotifications framework's -[unusernotificationcenterdelegate didreceivenotificationresponse:withcompletionhandler:]") __tvos_prohibited
{
 //在非本app界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
 nslog(@"%@----%@",identifier,notification);
 completionhandler();//处理完消息,最后一定要调用这个代码块
}
*/

 还有推送插件开发: 类似ios tody widget插件开发

iOS10全新推送功能实现代码

iOS10全新推送功能实现代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部