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

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

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

延伸 · 阅读

精彩推荐
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09