服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS10全新推送功能实现代码

iOS10全新推送功能实现代码

2021-01-28 16:21u012847940 IOS

这篇文章主要为大家详细介绍了iOS10全新推送功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

从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全新推送功能实现代码

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

延伸 · 阅读

精彩推荐
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03