上個(gè)月接到一個(gè)需求,做ios10的推送,意圖沖擊AppStore頭條.瞬間抓狂,工具都還沒(méi)有,于是趕緊安裝xcodeBeta版,ios10Beta版,然后就開(kāi)始無(wú)盡的查資料,畢竟新功能,畢竟沒(méi)做過(guò)........不過(guò)還好,在發(fā)布會(huì)之前趕出來(lái)了,由于本人比較懶,拖到現(xiàn)在才寫(xiě)出來(lái),接下來(lái)就是見(jiàn)證奇跡的時(shí)刻!
原理
圖中,Provider是指某個(gè)iPhone軟件的Push服務(wù)器,這篇文章我將使用.net作為Provider。
APNS 是Apple Push Notification Service(Apple Push服務(wù)器)的縮寫(xiě),是蘋(píng)果的服務(wù)器。
上圖可以分為三個(gè)階段。
第一階段:.net應(yīng)用程序把要發(fā)送的消息、目的iPhone的標(biāo)識(shí)打包,發(fā)給APNS。
第二階段:APNS在自身的已注冊(cè)Push服務(wù)的iPhone列表中,查找有相應(yīng)標(biāo)識(shí)的iPhone,并把消息發(fā)到iPhone。
第三階段:iPhone把發(fā)來(lái)的消息傳遞給相應(yīng)的應(yīng)用程序, 并且按照設(shè)定彈出Push通知。
從上圖我們可以看到。
1、首先是應(yīng)用程序注冊(cè)消息推送。
2、IOS跟APNS Server要deviceToken。應(yīng)用程序接受deviceToken。
3、應(yīng)用程序?qū)eviceToken發(fā)送給PUSH服務(wù)端程序。
4、 服務(wù)端程序向APNS服務(wù)發(fā)送消息。
5、APNS服務(wù)將消息發(fā)送給iPhone應(yīng)用程序。
xcode8以后測(cè)試環(huán)境證書(shū)就可以自動(dòng)生成了,所以就不再多說(shuō).
創(chuàng)建
很久以前寫(xiě)過(guò)ios9的today extension,與其類似,同樣需要?jiǎng)?chuàng)建一個(gè)target,
如圖,Notification Content負(fù)責(zé)自定義通知UI,Notification Service Extension負(fù)責(zé)接收并處理數(shù)據(jù)
正活
ios的拓展類是不能獨(dú)立聯(lián)網(wǎng)請(qǐng)求數(shù)據(jù)的,但是之前做today的時(shí)候查過(guò)一些別的app,下拉通知欄的時(shí)候有些app可以抓到包,估計(jì)是單獨(dú)做了一個(gè)網(wǎng)絡(luò)請(qǐng)求的封裝,只是瞎猜,如有雷同,純屬巧合.但是通知就不用那么麻煩了.首先從網(wǎng)上隨便挑選一張圖片(限定https協(xié)議): https://homeba.s3.amazonaws.com/__sized__/scene/2c0f3bdb7715fed7190fd87e5e5340e4-1473387950-crop-c0-5__0-5-590x442-85.jpg ,推送格式和可以自選,詳情可見(jiàn): https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html .
上圖是我用的格式,"aps"內(nèi)部的內(nèi)容ios會(huì)自動(dòng)獲取歸類.
alert是通知的文字內(nèi)容,
sound是通知聲音,在這取默認(rèn),
badge是否顯示程序徽章,
重點(diǎn)是mutable-content,這個(gè)字段決定了調(diào)用自定義通知界面,也就是Notification Content控制器
category是和后臺(tái)商量好的一個(gè)值,顯示action,也就是通知下面的按鈕,可以擴(kuò)展一些操作而不必進(jìn)入程序.
"aps"外部就可以添加一些需要的字段,這就看具體需求了.
推送工具方面的準(zhǔn)備工作就已經(jīng)完成了,下面開(kāi)始代碼干貨.
- (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);}
因?yàn)椴荒芊奖愕氖褂肧DImage框架,所以網(wǎng)絡(luò)請(qǐng)求只能自己松手,豐衣足食,另外,ios10通知雖然可以加載圖片,但是只能加載本地的圖片,所以這里需要做一個(gè)處理,先把圖片請(qǐng)求下來(lái),再存到本地
- (UIImage *) getImageFromURL:(NSString *)fileURL {NSLog(@"執(zhí)行圖片下載函數(shù)");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;}
然后數(shù)據(jù)方便就已經(jīng)完成了,最后一步,自定義UI并獲取圖片.自帶的storyboard還是很好用的.
做好約束就可以加圖片啦!
- (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];}}
就是這么簡(jiǎn)單,大功告成!!!(松一大口氣.....)
來(lái)一個(gè)效果圖,哈哈
本文已被整理到了《iOS推送教程》,歡迎大家學(xué)習(xí)閱讀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選