当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS 10 推送高阶篇(必看)

iOS 10 推送高阶篇(必看)

2019年07月24日  | 移动技术网IT编程  | 我要评论

潜水艇洗衣机地漏,东京申奥,fish8000

推荐阅读:

ios10推送之基础知识(必看篇)

这篇文章开始,我会跟大家好好讲讲,苹果新发布的ios10的所有通知类。

一、创建本地通知事例详解:

注意啊,小伙伴们,本地通知也必须在appdelegate中注册中心,通知的开关打不打开无所谓的,毕竟是本地通知,但是通知的接收的代理,以及通知点击的代理,苹果给合二为一了。所以大家还是需要在appdelegate中写上这2个方法,还有不要忘记在- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions注册通知中心。如果使用极光推送的小伙伴,写看一下我的基础篇,辛苦大家啦

创建一个unnotificationrequest类的实例,一定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是可以用来区分这个通知与其他通知

把request加到unusernotificationcenter, 并设置触发器,等待触发
如果另一个request具有和之前request相同的标识,不同的内容, 可以达到更新通知的目的

创建一个本地通知我们应该先创建一个unnotificationrequest类,并且将这个类添加到unusernotificationcenter才可以。代码如下:

// 1.创建一个unnotificationrequest
nsstring *requestidentifer = @"testrequest";
unnotificationrequest *request = [unnotificationrequest requestwithidentifier:requestidentifer content:content trigger:trigger];
// 2.将unnotificationrequest类,添加进当前通知中心中
[[unusernotificationcenter currentnotificationcenter] addnotificationrequest:request withcompletionhandler:^(nserror * _nullable error) {
}];

在创建unnotificationrequest类时,官方的解释是说,一个通知请求可以在预定通过时间和位置,来通知用户。触发的方式见unnotificationtrigger的相关说明。调用该方法,在通知触发的时候。会取代具有相同标识符的通知请求,此外,消息个数受系统限制。

上面的翻译,看上去可能有些拗口,简单来说,就是我们需要为unnotificationrequest设置一个标识符,通过标识符,我们可以对该通知进行添加,删除,更新等操作。

以下是完整的创建通知的代码:

// 1.创建通知内容
unmutablenotificationcontent *content = [[unmutablenotificationcontent alloc] init];
content.title = @"徐不同测试通知";
content.subtitle = @"测试通知";
content.body = @"来自徐不同的简书";
content.badge = @1;
nserror *error = nil;
nsstring *path = [[nsbundle mainbundle] pathforresource:@"icon_certification_status1@2x" oftype:@"png"];
// 2.设置通知附件内容
unnotificationattachment *att = [unnotificationattachment attachmentwithidentifier:@"att1" url:[nsurl fileurlwithpath:path] options:nil error:&error];
if (error) {
nslog(@"attachment error %@", error);
}
content.attachments = @[att];
content.launchimagename = @"icon_certification_status1@2x";
// 2.设置声音
unnotificationsound *sound = [unnotificationsound defaultsound];
content.sound = sound;
// 3.触发模式
untimeintervalnotificationtrigger *trigger = [untimeintervalnotificationtrigger triggerwithtimeinterval:5 repeats:no];
// 4.设置unnotificationrequest
nsstring *requestidentifer = @"testrequest";
unnotificationrequest *request = [unnotificationrequest requestwithidentifier:requestidentifer content:content trigger:trigger1];
//5.把通知加到unusernotificationcenter, 到指定触发点会被触发
[[unusernotificationcenter currentnotificationcenter] addnotificationrequest:request withcompletionhandler:^(nserror * _nullable error) {
}];

通过以上代码,我们就可以创建一个5秒触发本地通知,具体样式可以看下图

下拉放大content.launchimagename = @”icon_certification_status1@2x”;显示的图片是这行代码的效果,如图

根据上面内容,大家会发现在创建unnotificationrequest的时候,会需要unmutablenotificationcontent以及untimeintervalnotificationtrigger这两个类。下面我就对相关的类,以及类扩展,做相应的说明。

1.unnotificationcontent以及unmutablenotificationcontent(通知内容和可变通知内容)

通知内容分为可变的以及不可变的两种类型,类似于可变数组跟不可变数组。后续我们通过某一特定标识符更新通知,便是用可变通知了。

不管是可变通知还是不可变通知,都有以下的几个属性:

// 1.附件数组,存放unnotificationattachment类
@property (ns_nonatomic_iosonly, copy) nsarray *attachments ;
// 2.应用程序角标,0或者不传,意味着角标消失
@property (ns_nonatomic_iosonly, copy, nullable) nsnumber *badge;
// 3.主体内容
@property (ns_nonatomic_iosonly, copy) nsstring *body ;
// 4.app通知下拉预览时候展示的图
@property (ns_nonatomic_iosonly, copy) nsstring *launchimagename;
// 5.unnotificationsound类,可以设置默认声音,或者指定名称的声音
@property (ns_nonatomic_iosonly, copy, nullable) unnotificationsound *sound ;
// 6.推送内容的子标题
@property (ns_nonatomic_iosonly, copy) nsstring *subtitle ;
// 7.通知线程的标识
@property (ns_nonatomic_iosonly, copy) nsstring *threadidentifier;
// 8.推送内容的标题
@property (ns_nonatomic_iosonly, copy) nsstring *title ;
// 9.远程通知推送内容
@property (ns_nonatomic_iosonly, copy) nsdictionary *userinfo;
// 10.category标识
@property (ns_nonatomic_iosonly, copy) nsstring *categoryidentifier;

以上的的属性,我都增加了相应的说明,大家可以对照我的注释来使用。

2.unnotificationattachment (附件内容通知)

在unnotificationcontent类中,有个附件数组的属性,这就是包含unnotificationattachment类的数组了。

@property (ns_nonatomic_iosonly, copy) nsarray *attachments ;

苹果的解释说,unnotificationattachment(附件通知)是指可以包含音频,图像或视频内容,并且可以将其内容显示出来的通知。使用本地通知时,可以在通知创建时,将附件加入即可。对于远程通知,则必须实现使用unnotificationserviceextension类通知服务扩展。

创建附件的方法是attachmentwithidentifier:url:options:error:。在使用时,必须指定使用文件附件的内容,并且文件格式必须是支持的类型之一。创建附件后,将其分配给内容对象的附件属性。 (对于远程通知,您必须从您的服务扩展做到这一点。)

附件通知支持的类型如下图:

下面是创建unnotificationattachment的方法:

+ (nullable instancetype)attachmentwithidentifier:(nsstring *)identifier url:(nsurl *)url options:(nullable nsdictionary *)options error:(nserror *__nullable *__nullable)error;

注意:url必须是一个有效的文件路径,不然会报错

这里我再在说下options的属性,一共有4种选项(这几个属性可研究死我了)

1unnotificationattachmentoptionstypehintkey此键的值是一个包含描述文件的类型统一类型标识符(uti)一个nsstring。如果不提供该键,附件的文件扩展名来确定其类型,常用的类型标识符有

kuttypeimage,kuttypejpeg2000,kuttypetiff,kuttypepict,kuttypegif ,kuttypepng,kuttypequicktimeimage等。看到这里你一定有疑问,这些类型导入报错了啊!!我研究了苹果文档,发现大家需要添加以下框架才可以,具体大家可以通过以下类型来处理。

注意:

框架就是#import

使用方法如下:

dict[unnotificationattachmentoptionstypehintkey] = (__bridge id _nullable)(kuttypeimage);

2unnotificationattachmentoptionsthumbnailhiddenkey,是一个bool值,为yes时候,缩略图将隐藏,默认为yes。如图:

大家可以对照上面的图来看,就明白是哪里的图消失了。

使用方法如下:

dict[unnotificationattachmentoptionsthumbnailhiddenkey] = @yes;

3unnotificationattachmentoptionsthumbnailclippingrectkey剪贴矩形的缩略图。这个密钥的值是包含一个归一化的cgrect – 也就是说,一个单元的矩形,其值是在以1.0〜 0.0 ,表示要显示的原始图像的所述部分的字典。例如,指定的(0.25 , 0.25)的原点和大小(0.5 ,0.5 )定义了剪辑矩形,只显示图像的中心部分。使用cgrectcreatedictionaryrepresentation函数来创建字典的矩形。

上面这句话是苹果的翻译,太绕口了。我简单说,就是我下面这幅图。

整张图被分割了,整体比例为1,如果想得到图中阴影面积,就需要写的cgrect(0.5,0.5,0.25,0.25),意思是,从(0.5,0.5)为原点,面积为(0.25,0.25),大家可以理解成,即下面的方法。

使用方法如下:

dict[unnotificationattachmentoptionsthumbnailclippingrectkey] = (__bridge id _nullable)((cgrectcreatedictionaryrepresentation(cgrectmake(0.5, 0.5, 0.25 ,0.25))));;

使用上面的方法,可以得到一张图的阴影部分的图像,这张图像会是通知的缩略图。比如我下面的这个图,缩略图大家应该可以发现变了吧。

这里为了理解,在给大家说几个”坐标点”:

(0,0,0.25,0.25)左上角的最小正方形

(0,0,0.5,0.5) 四分之一的正方形,左上角

(0.5,0.5,0.5,0.5)四分之一的正方形,右下角

(0.5,0,0.5,0.5)四分之一的正方形,左下角

(0.25,0.25,0.5,0.5)最中心的正方形

特别注意:

调试到这里的时候,我感觉苹果应该是有个bug,就是我在来回变化这个显示缩略图的frame的时候,来回改,永远显示为第一次写的frame。我在修改unnotificationrequest的requestidentifer属性后,可以变换属性。所以我猜测可能相同requestidentifer的通知,算一个通知,所以只能调用更新的方法,来变化缩略图的吃不腻吧,或许也不是bug。

4unnotificationattachmentoptionsthumbnailtimekey,一般影片附件会用到,指的是用影片中的某一秒来做这个缩略图;

使用方法如下:

dict[unnotificationattachmentoptionsthumbnailtimekey] =@10;

这里我们可以直接传递一个nsnumber的数值,比如使用影片第10s的画面来做缩略图就按照上面的来写。此外,要注意的是,这个秒数必须是这个影片长度范围内的,不然报错。

3.untimeintervalnotificationtrigger (通知触发模式)

这个我在!(这篇文章中已经初步介绍了,现在我在详细介绍下)[www.baidu.com]这篇文章中已经初步介绍了,现在我在详细介绍下。

1.unpushnotificationtrigger (远程通知触发)一般我们不会使用的

2.untimeintervalnotificationtrigger (本地通知) 一定时间之后,重复或者不重复推送通知。我们可以设置timeinterval(时间间隔)和repeats(是否重复)。

使用方法:

untimeintervalnotificationtrigger *triggerone = [untimeintervalnotificationtrigger triggerwithtimeinterval:5 repeats:no];

解释:上面的方法是指5秒钟之后执行。repeats这个属性,如果需要为重复执行的,则timeinterval必须大于60s,否则会报* terminating app due to uncaught exception 'nsinternalinconsistencyexception', reason: 'time interval must be at least 60 if repeating'的错误!**

3.uncalendarnotificationtrigger(本地通知) 一定日期之后,重复或者不重复推送通知 例如,你每天8点推送一个通知,只需要datecomponents为8。如果你想每天8点都推送这个通知,只要repeats为yes就可以了。

// 周一早上 8:00 上班
nsdatecomponents *components = [[nsdatecomponents alloc] init];
// 注意,weekday是从周日开始的,如果想设置为从周一开始,大家可以自己想想~
components.weekday = 2;
components.hour = 8;
uncalendarnotificationtrigger *trigger = [uncalendarnotificationtrigger triggerwithdatematchingcomponents:components repeats:yes];

4.unlocationnotificationtrigger (本地通知)地理位置的一种通知,使用这个通知,你需要导入

#import这个系统类库。示例代码如下:
//1、如果用户进入或者走出某个区域会调用下面两个方法
- (void)locationmanager:(cllocationmanager *)manager
didenterregion:(clregion *)region
- (void)locationmanager:(cllocationmanager *)manager
didexitregion:(clregion *)region代理方法反馈相关信息
//2、一到某个经纬度就通知,判断包含某一点么
// 不建议使用!!!!!!clregion *region = [[clregion alloc] init];// 不建议使用!!!!!!
clcircularregion *circlarregin = [[clcircularregion alloc] init];
[circlarregin containscoordinate:(cllocationcoordinate2d)];
unlocationnotificationtrigger *trigger4 = [unlocationnotificationtrigger triggerwithregion:circlarregin repeats:no];

注意,这里建议使用clcircularregion这个继承自clregion的类,因为我看到苹果已经飞起了clregion里面是否包含这一点的方法,并且推荐我们使用clcircularregion这个类型

以上所述是小编给大家介绍的ios 10 推送高阶篇(必看),希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网