注意!!!
Media Attachments和自定義推送界面
本地推送和遠程推送同時都可支持附帶Media Attachments。不過遠程通知需要實現通知服務擴展UNNotificationServiceExtension,在service extension里面去下載attachment,但是需要注意,service extension會限制下載的時間(30s),並且下載的文件大小也會同樣被限制。這里畢竟是一個推送,而不是把所有的內容都推送給用戶。所以你應該去推送一些縮小比例之后的版本。比如圖片,推送里面附帶縮略圖,當用戶打開app之后,再去下載完整的高清圖。視頻就附帶視頻的關鍵幀或者開頭的幾秒,當用戶打開app之后再去下載完整視頻。
attachment支持圖片,音頻,視頻,附件支持的類型及大小

系統會在通知注冊前校驗附件,如果附件出問題,通知注冊失敗;校驗成功后,附件會轉入attachment data store;如果附件是在app bundle,則是會被copy來取代move
media attachments可以利用3d touch進行預覽和操作
attachment data store的位置?利用代碼測試 獲取在磁盤上的圖片文件作為attachment,會發現注冊完通知后,圖片文件被移除,在app的沙盒中找不到該文件在哪里; 想要獲取已存在的附件內容,文檔中提及可以通過UNUserNotificationCenter中方法,但目前文檔中這2個方法還是灰的,見蘋果開發者文檔

//就是這兩個方法 getDataForAttachment:withCompletionHandler: getReadFileHandleForAttachment:withCompletionHandler:
1、准備工作
附件限定https協議,所以我們現在找一個支持https的圖床用來測試,我之前能測試的圖床現在不能用了。你們可以自行googole,這是我之前上傳的圖片鏈接:https://p1.bpimg.com/524586/475bc82ff016054ds.jpg
具體附件格式可以查看蘋果開發文檔
2、添加新的Targe--> Notification Service
先在Xcode 打開你的工程,File-->New-->Targe然后添加這個Notification Service:

這樣在你工程里能看到下面目錄:

然后會自動創建一個 UNNotificationServiceExtension 的子類 NotificationService,通過完善這個子類,來實現你的需求。
點開 NotificationService.m 會看到 2 個方法:
// Call contentHandler with the modified notification content to deliver. If the handler is not called before the service's time expires then the unmodified notification will be delivered. // You are expected to override this method to implement push notification modification. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler; // Will be called just before this extension is terminated by the system. You may choose whether to override this method. - (void)serviceExtensionTimeWillExpire;
didReceiveNotificationRequest
讓你可以在后台處理接收到的推送,傳遞最終的內容給 contentHandlerserviceExtensionTimeWillExpire
在你獲得的一小段運行代碼的時間即將結束的時候,如果仍然沒有成功的傳入內容,會走到這個方法,可以在這里傳肯定不會出錯的內容,或者他會默認傳遞原始的推送內容
主要的思路就是在這里把附件下載下來,然后才能展示渲染,下面是下載保存的相關方法:
- (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 firstObject]; 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); } - (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; } - (void)serviceExtensionTimeWillExpire { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. self.contentHandler(self.bestAttemptContent); }
apes如下:
{
"aps":{ "alert" : { "title" : "iOS遠程消息,我是主標題!-title", "subtitle" : "iOS遠程消息,我是主標題!-Subtitle", "body" : "Dely,why am i so handsome -body" }, "sound" : "default", "badge" : "1", "mutable-content" : "1", "category" : "Dely_category" }, "image" : "https://p1.bpimg.com/524586/475bc82ff016054ds.jpg", "type" : "scene", "id" : "1007" }
注意:mutable-content這個鍵值為1,這意味着此條推送可以被 Service Extension 進行更改,也就是說要用Service Extension需要加上這個鍵值為1.
3、添加新的Targe--> Notification Content
先在Xcode 打開你的工程,File-->New-->Targe然后添加這個 Notification Content:

這樣你在工程里同樣看到下面的目錄:

點開 NotificationViewController.m 會看到 2 個方法:
- (void)viewDidLoad; - (void)didReceiveNotification:(UNNotification *)notification;
前者渲染UI,后者獲取通知信息,更新UI控件中的數據。
在MainInterface.storyboard中自定你的UI頁面,可以隨意發揮,但是這個UI見面只能用於展示,並不能響應點擊或者手勢其他事件,只能通過category來實現,下面自己添加view和約束

然后把view拉到.m文件中,代碼如下:
#import "NotificationViewController.h" #import <UserNotifications/UserNotifications.h> #import <UserNotificationsUI/UserNotificationsUI.h> @interface NotificationViewController () <UNNotificationContentExtension> @property IBOutlet UILabel *label; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation NotificationViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any required interface initialization here. // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // [self.view addSubview:view]; // view.backgroundColor = [UIColor redColor]; } - (void)didReceiveNotification:(UNNotification *)notification { 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]; } } @end
有人要有疑問了,可不可以不用storyboard來自定義界面?當然可以了!
只需要在Notifications Content 的info.plist中把NSExtensionMainStoryboard
替換為NSExtensionPrincipalClass
,並且value對應你的類名!
然后在viewDidLoad里用純代碼布局就可以了

4、發送推送
完成上面的工作的時候基本上可以了!然后運行工程,
上面的json數據放到APNS Pusher里面點擊send:

稍等片刻應該能收到消息:

長按或者右滑查看:

注意 注意 注意:
如果你添加了category,需要在Notification content的info.plist添加一個鍵值對UNNotificationExtensionCategory
的value值和category Action的category值保持一致就行。

同時在推送json中添加category鍵值對也要和上面兩個地方保持一致:

就變成了下面:

上面介紹了遠端需要Service Extension 的遠端推送
iOS 10附件通知(圖片、gif、音頻、視頻)。不過對圖片和視頻的大小做了一些限制(圖片不能超過 10M,視頻不能超過 50M),而且附件資源必須存在本地,如果是遠程推送的網絡資源需要提前下載到本地。
如果是本地的就簡單了只需要在Service Extension的NotificationService.m的- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler
拿到資源添加到Notification Content,在Notification Content的控制器取到資源自己來做需求處理和展示
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // 資源路徑 NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"]; // 創建附件資源 // * identifier 資源標識符 // * URL 資源路徑 // * options 資源可選操作 比如隱藏縮略圖之類的 // * error 異常處理 UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"video.attachment" URL:videoURL options:nil error:nil]; // 將附件資源添加到 UNMutableNotificationContent 中 if (attachment) { self.bestAttemptContent.attachments = @[attachment]; } self.contentHandler(self.bestAttemptContent); }

上圖如果你想把default 隱藏掉,只需要在Notification Content 的info.plist中添加一個鍵值UNNotificationExtensionDefaultContentHidden
設置為YES就可以了:

原文鏈接:http://www.jianshu.com/p/81c6bd16c7ac
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。