当前位置:首页 > 通信资讯 > 正文

ios10发布(ios推送设置)

上个月接到一个需求,做ios10的推送,意图冲击appstore头条.瞬间抓狂,工具都还没有,于是赶紧安装xcodebeta版,ios10beta版,然后就开始无尽的查资料,毕竟新功能,毕竟没做过........不过还好,在发布会之前赶出来了,由于本人比较懒,拖到现在才写出来,接下来就是见证奇迹的时刻!

原理

ios10发布(ios推送设置)

图中,provider是指某个iphone软件的push服务器,这篇文章我将使用.net作为provider。

apns 是apple push notification service(apple push服务器)的缩写,是苹果的服务器。

上图可以分为三个阶段。

第一阶段:.net应用程序把要发送的消息、目的iphone的标识打包,发给apns。

第二阶段:apns在自身的已注册push服务的iphone列表中,查找有相应标识的iphone,并把消息发到iphone。

第三阶段:iphone把发来的消息传递给相应的应用程序, 并且按照设定弹出push通知。

ios10发布(ios推送设置)

从上图我们可以看到。

1、首先是应用程序注册消息推送。

2、ios跟apns server要devicetoken。应用程序接受devicetoken。

3、应用程序将devicetoken发送给push服务端程序。

4、 服务端程序向apns服务发送消息。

5、apns服务将消息发送给iphone应用程序。

xcode8以后测试环境证书就可以自动生成了,所以就不再多说.

创建

很久以前写过ios9的today extension,与其类似,同样需要创建一个target,

ios10发布(ios推送设置)

如图,notification content负责自定义通知ui,notification service extension负责接收并处理数据

ios10发布(ios推送设置)

正活
ios的拓展类是不能独立联网请求数据的,但是之前做today的时候查过一些别的app,下拉通知栏的时候有些app可以抓到包,估计是单独做了一个网络请求的封装,只是瞎猜,如有雷同,纯属巧合.但是通知就不用那么麻烦了.首先从网上随便挑选一张图片(限定https协议): https://homeba.s3.amazonaws.com/__sized__/scene/2c0f3bdb7715fed7190fd87e5e5340e4-1473387950-crop-c0-5__0-5-590x442-85.jpg ,推送格式和可以自选,详情可见: https://developer.apple.com/library/ios/documentation/networkinginternet/conceptual/remotenotificationspg/chapters/thenotificationpayload.html .

ios10发布(ios推送设置)

上图是我用的格式,"aps"内部的内容ios会自动获取归类.

alert是通知的文字内容,

sound是通知声音,在这取默认,

badge是否显示程序徽章,

重点是mutable-content,这个字段决定了调用自定义通知界面,也就是notification content控制器

category是和后台商量好的一个值,显示action,也就是通知下面的按钮,可以扩展一些操作而不必进入程序.

"aps"外部就可以添加一些需要的字段,这就看具体需求了.

推送工具方面的准备工作就已经完成了,下面开始代码干货.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 - (void)didreceivenotificationrequest:(unnotificationrequest *)request withcontenthandler:(void (^)(unnotificationcontent * _nonnull))contenthandler { self.contenthandler = contenthandler; self.bestattemptcontent = [request.content mutablecopy]; nsstring * attchurl = [request.content.userinfo objectforkey:@"image"]; //下载图片,放到本地 uiimage * imagefromurl = [self getimagefromurl:attchurl]; //获取documents目录 nsarray * paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectorypath = [paths objectatindex:0]; nsstring * localpath = [self saveimage:imagefromurl withfilename:@"myimage" oftype:@"png" indirectory:documentsdirectorypath]; if (localpath && ![localpath isequaltostring:@""]) { unnotificationattachment * attachment = [unnotificationattachment attachmentwithidentifier:@"photo" url:[nsurl urlwithstring:[@"file://" stringbyappendingstring:localpath]] options:nil error:nil]; if (attachment) { self.bestattemptcontent.attachments = @[attachment]; } } self.contenthandler(self.bestattemptcontent); }

因为不能方便的使用sdimage框架,所以网络请求只能自己松手,丰衣足食,另外,ios10通知虽然可以加载图片,但是只能加载本地的图片,所以这里需要做一个处理,先把图片请求下来,再存到本地

?
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 - (uiimage *) getimagefromurl:(nsstring *)fileurl { nslog(@"执行图片下载函数"); uiimage * result; //datawithcontentsofurl方法需要https连接 nsdata * data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:fileurl]]; result = [uiimage imagewithdata:data]; return result; } //将所下载的图片保存到本地 -(nsstring *) saveimage:(uiimage *)image withfilename:(nsstring *)imagename oftype:(nsstring *)extension indirectory:(nsstring *)directorypath { nsstring *urlstr = @""; if ([[extension lowercasestring] isequaltostring:@"png"]) { urlstr = [directorypath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@.%@", imagename, @"png"]]; [uiimagepngrepresentation(image) writetofile:urlstr options:nsatomicwrite error:nil]; } else if ([[extension lowercasestring] isequaltostring:@"jpg"] || [[extension lowercasestring] isequaltostring:@"jpeg"]) { urlstr = [directorypath stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@.%@", imagename, @"jpg"]]; [uiimagejpegrepresentation(image, 1.0) writetofile:urlstr options:nsatomicwrite error:nil]; } else { nslog(@"extension error"); } return urlstr; }

然后数据方便就已经完成了,最后一步,自定义ui并获取图片.自带的storyboard还是很好用的.

ios10发布(ios推送设置)

做好约束就可以加图片啦!

?
1 2 3 4 5 6 7 8 9 - (void)didreceivenotification:(unnotification *)notification { nslog(@"嘿嘿"); self.label.text = notification.request.content.body; unnotificationcontent * content = notification.request.content; unnotificationattachment * attachment = content.attachments.firstobject; if (attachment.url.startaccessingsecurityscopedresource) { self.imageview.image = [uiimage imagewithcontentsoffile:attachment.url.path]; } }

就是这么简单,大功告成!!!(松一大口气.....)

来一个效果图,哈哈

ios10发布(ios推送设置)

ios10发布(ios推送设置)

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

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。