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

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

服务器之家 - 编程语言 - IOS - iOS10 推送最新特性研究

iOS10 推送最新特性研究

2021-01-29 15:30幸福小祢 IOS

这篇文章主要为大家详细研究了iOS10 推送的最新特性,推送内容更加丰富,感兴趣的小伙伴们可以参考一下

最近在研究ios10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点:

 1.推送内容更加丰富,由之前的alert 到现在的title, subtitle, body
 2.推送统一由trigger触发
 3.可以为推送增加附件,如图片、音频、视频,这就使推送内容更加丰富多彩
 4.可以方便的更新推送内容 

import 新框架

添加新的框架 usernotifications.framework

 iOS10 推送最新特性研究

#import <usernotifications/usernotifications.h> 

注册推送

在设置通知的时候,需要先进行注册,获取授权
ios10 所有通知都是通过unusernotificationcenter来管理,包括远程通知和本地通知

?
1
2
3
4
5
6
7
8
9
10
11
//ios8以下
[application registerforremotenotificationtypes:uiremotenotificationtypebadge | uiremotenotificationtypealert | uiremotenotificationtypesound];
 
//ios8 - ios10
[application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert | uiusernotificationtypesound | uiusernotificationtypebadge categories:nil]];
 
//ios10
unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
[center requestauthorizationwithoptions:(unauthorizationoptionalert | unauthorizationoptionbadge | unauthorizationoptionsound) completionhandler:^(bool granted, nserror * _nullable error) {
 
}

获取用户设置

ios10 提供了获取用户授权相关设置信息的接口getnotificationsettingswithcompletionhandler: , 回调带有一个unnotificationsettings对象,它具有以下属性,可以准确获取各种授权信息

authorizationstatus
soundsetting
badgesetting
alertsetting
notificationcentersetting
lockscreensetting
carplaysetting
alertstyle 

像下面的方法,点击allow

iOS10 推送最新特性研究

?
1
2
3
4
5
6
7
8
9
10
11
12
13
unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
[center requestauthorizationwithoptions:(unauthorizationoptionalert | unauthorizationoptionbadge | unauthorizationoptionsound) completionhandler:^(bool granted, nserror * _nullable error) {
   if (granted) {
      //点击允许
      nslog(@"注册通知成功");
      [center getnotificationsettingswithcompletionhandler:^(unnotificationsettings * _nonnull settings) {
      nslog(@"%@", settings);
      }];
    } else {
      //点击不允许
      nslog(@"注册通知失败");
    }
  }];

打印信息:   *<unnotificationsettings: 0x174090a90; authorizationstatus: authorized, notificationcentersetting: enabled, soundsetting: enabled, badgesetting: enabled, lockscreensetting: enabled, alertsetting: notsupported, carplaysetting: enabled, alertstyle: banner>* 

注册apns, 获取token

ios10, 注册apns和获取token的方法还和之前一样
application: didfinishlaunchingwithoptions:调用 registerforremotenotifications方法
 [[uiapplication sharedapplication] registerforremotenotifications]; 

在代理方法application: didregisterforremotenotificationswithdevicetoken:中获取token

?
1
2
3
4
5
6
7
8
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken ns_available_ios(3_0){
    nslog(@"devicetoken:%@",devicetoken);
  }
 
- (void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error ns_available_ios(3_0){
    nslog(@"didfailtoregisterforremotenotificationswitherror:%@",error);
  }

设置处理通知的action 和 category 

在ios8以前是没有category这个属性的;
在ios8注册推送,获取授权的时候,可以一并设置category, 注册的方法直接带有这个参数;
在ios10, 需要调用一个方法setnotificationcategories:来为管理推送的unusernotificationcenter实例设置category, category又可以对应设置action;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//设置category
//unnotificationactionoptionauthenticationrequired 需要解锁
//unnotificationactionoptiondestructive 显示为红色
//unnotificationactionoptionforeground  点击打开app
 
unnotificationaction *action1 = [unnotificationaction actionwithidentifier:@"action1" title:@"策略1行为1" options:unnotificationactionoptionforeground];
 
untextinputnotificationaction *action2 = [untextinputnotificationaction actionwithidentifier:@"action2" title:@"策略1行为2" options:unnotificationactionoptiondestructive textinputbuttontitle:@"comment" textinputplaceholder:@"reply"];
 
 //unnotificationcategoryoptionnone
 //unnotificationcategoryoptioncustomdismissaction 清除通知被触发会走通知的代理方法
 //unnotificationcategoryoptionallowincarplay    适用于行车模式
unnotificationcategory *category1 = [unnotificationcategory categorywithidentifier:@"category1" actions:@[action2,action1] minimalactions:@[action2,action1] intentidentifiers:@[] options:unnotificationcategoryoptioncustomdismissaction];
 
unnotificationaction *action3 = [unnotificationaction actionwithidentifier:@"action3" title:@"策略2行为1" options:unnotificationactionoptionforeground];
 
unnotificationaction *action4 = [unnotificationaction actionwithidentifier:@"action4" title:@"策略2行为2" options:unnotificationactionoptionforeground];
unnotificationcategory *category2 = [unnotificationcategory categorywithidentifier:@"category2" actions:@[action3,action4] minimalactions:@[action3,action4] intentidentifiers:@[] options:unnotificationcategoryoptioncustomdismissaction];
 
[[unusernotificationcenter currentnotificationcenter] setnotificationcategories:[nsset setwithobjects:category1,category2, nil]];

设置通知内容

因为ios10远程通知与本地通知统一起来了,通知内容属性是一致的,不过远程推送就需要在payload进行具体设置了,下面以本地通知为例,介绍关于unnotificationcontent的内容
官网上明确说明了,我们是不能直接创建unnotificationcontent的实例的, 如果我们需要自己去配置内容的各个属性,我们需要用到unmutablenotificationcontent
看一下它的一些属性:
attachments          //附件
badge                //徽标
body                 //推送内容body
categoryidentifier   //category标识
launchimagename      //点击通知进入应用的启动图
sound               //声音
subtitle            //推送内容子标题
title               //推送内容标题
userinfo           //远程通知内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unmutablenotificationcontent *content = [[unmutablenotificationcontent alloc] init];
  content.title = @"test";
  content.subtitle = @"1234567890";
  content.body = @"copyright © 2016年 jpush. all rights reserved.";
  content.badge = @1;
  nserror *error = nil;
  nsstring *path = [[nsbundle mainbundle] pathforresource:@"718835727" oftype:@"png"];
  unnotificationattachment *att = [unnotificationattachment attachmentwithidentifier:@"att1" url:[nsurl fileurlwithpath:path] options:nil error:&error];
  if (error) {
    nslog(@"attachment error %@", error);
  }
  content.attachments = @[att];
  content.categoryidentifier = @"category1”; //这里设置category1, 是与之前设置的category对应
  content.launchimagename = @"1-eb_0ovtcxjxhz7-ioobsaq";
 
unnotificationsound *sound = [unnotificationsound defaultsound];
content.sound = sound;

 iOS10 推送最新特性研究

通知触发器

unnotificationtrigger
ios 10触发器有4种
 •unpushnotificationtrigger 触发apns服务,系统自动设置(这是区分本地通知和远程通知的标识)
 •untimeintervalnotificationtrigger 一段时间后触发
 •uncalendarnotificationtrigger 指定日期触发
 •unlocationnotificationtrigger 根据位置触发,支持进入某地或者离开某地或者都有

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//十秒后
untimeintervalnotificationtrigger *trigger1 = [untimeintervalnotificationtrigger triggerwithtimeinterval:10 repeats:no];
 
//每周日早上8:00
nsdatecomponents *component = [[nsdatecomponents alloc] init];
component.weekday = 1;
component.hour = 8;
uncalendarnotificationtrigger *trigger2 = [uncalendarnotificationtrigger triggerwithdatematchingcomponents:component repeats:yes];
 
//圆形区域,进入时候进行通知
cllocationcoordinate2d cen = cllocationcoordinate2dmake(80.335400, -90.009201);
clcircularregion *region = [[clcircularregion alloc] initwithcenter:cen
                               radius:500.0 identifier:@“center"];
region.notifyonentry = yes; //进入的时候
region.notifyonexit = no;  //出去的时候
unlocationnotificationtrigger *trigger3 = [unlocationnotificationtrigger
  triggerwithregion:region repeats:no];

添加通知 / 更新通知

 1.创建一个unnotificationrequest类的实例,一定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是可以用来区分这个通知与其他通知
 2.把request加到unusernotificationcenter, 并设置触发器,等待触发
 3.
如果另一个request具有和之前request相同的标识,不同的内容, 可以达到更新通知的目的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  nsstring *requestidentifer = @"testrequest";
  unnotificationrequest *request = [unnotificationrequest requestwithidentifier:requestidentifer content:content trigger:trigger1];
  //把通知加到unusernotificationcenter, 到指定触发点会被触发
  [center addnotificationrequest:request withcompletionhandler:^(nserror * _nullable error) {
  }];
 
 //在另外需要更新通知的地方
unmutablenotificationcontent *newcontent = [[unmutablenotificationcontent alloc] init];
newcontent.title = @"update";
newcontent.subtitle = @"xxxxxxxxx";
newcontent.body = @"copyright © 2016年 jpush. all rights reserved.";
untimeintervalnotificationtrigger *trigger1 = [untimeintervalnotificationtrigger triggerwithtimeinterval:3 repeats:no];
 unnotificationrequest *request = [unnotificationrequest requestwithidentifier:@"testrequest" content:newcontent trigger:trigger1];
[[unusernotificationcenter currentnotificationcenter] addnotificationrequest:request withcompletionhandler:^(nserror * _nullable error) {
 
}];

 iOS10 推送最新特性研究

获取和删除通知

这里通知是有两种状态
 •pending 等待触发的通知
 •delivered 已经触发展示在通知中心的通知

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//获取未触发的通知
[[unusernotificationcenter currentnotificationcenter] getpendingnotificationrequestswithcompletionhandler:^(nsarray<unnotificationrequest *> * _nonnull requests) {
  nslog(@"pending: %@", requests);
}];
 
//获取通知中心列表的通知
[[unusernotificationcenter currentnotificationcenter] getdeliverednotificationswithcompletionhandler:^(nsarray<unnotification *> * _nonnull notifications) {
  nslog(@"delivered: %@", notifications);
}];
 
 //清除某一个未触发的通知
 [[unusernotificationcenter currentnotificationcenter] removependingnotificationrequestswithidentifiers:@[@"testrequest1"]];
 //清除某一个通知中心的通知
 [[unusernotificationcenter currentnotificationcenter] removedeliverednotificationswithidentifiers:@[@"testrequest2"]];
 //对应的删除所有通知
[[unusernotificationcenter currentnotificationcenter] removeallpendingnotificationrequests];
[[unusernotificationcenter currentnotificationcenter] removealldeliverednotifications];

 delegate

<unusernotificationcenterdelegate> 

ios10收到通知不再是在application: didreceiveremotenotification:方法去处理, ios10推出新的代理方法,接收和处理各类通知(本地或者远程)

?
1
2
3
4
5
6
7
8
9
- (void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler {
  //应用在前台收到通知
  nslog(@"========%@", notification);
}
 
- (void)usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler {
  //点击通知进入应用
  nslog(@"response:%@", response);
}

最后 

下一篇文章继续介绍关于富媒体推送的 unnotificationserviceextension 和 notification content extension, 未完待续。。。

延伸 · 阅读

精彩推荐
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

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

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

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

    iOS 雷达效果实例详解

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

    SimpleWorld11022021-01-28
  • IOSiOS通过逆向理解Block的内存模型

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

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

    Swiftyper12832021-03-03
  • IOSiOS布局渲染之UIView方法的调用时机详解

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

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

    windtersharp7642021-05-04
  • IOS解析iOS开发中的FirstResponder第一响应对象

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

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

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

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

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

    daisy6092021-05-17
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

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

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

    xiari5772021-06-01
  • IOSIOS开发之字典转字符串的实例详解

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

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

    苦练内功5832021-04-01