上篇文章主要介紹了新的通知框架的基本使用,這篇文章主要說一下多媒體通知和擴展宪拥。
擴展(Notification Extension)
- Notification Service Extension
收到遠程推送通知之后仿野,我們可以利用Notification Service Extension這個擴展進行通知的修改。
建好target之后可以看到她君,系統(tǒng)就為我們實現了基本的方法脚作。我這里在收到通知之后修改了body
。
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
if bestAttemptContent.categoryIdentifier == "ceshi" {
bestAttemptContent.body = "\(bestAttemptContent.body) 加上修改內容了缔刹!"
}
contentHandler(bestAttemptContent)
}
}
override func 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.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
可以看到結果:
注:Notification Service Extension 現在只對遠程推送的通知有效球涛。因此Demo中我集成了極光推送,為了方便測試使用校镐。
喵神認為這個特性可以方便傳輸安全性的信息亿扁。下面是他的blog原話:
使用在本機截取推送并替換內容的方式,可以完成端到端 (end-to-end) 的推送加密鸟廓。你在服務器推送 payload 中加入加密過的文本从祝,在客戶端接到通知后使用預先定義或者獲取過的密鑰進行解密襟己,然后立即顯示。這樣一來牍陌,即使推送信道被第三方截取稀蟋,其中所傳遞的內容也還是安全的。使用這種方式來發(fā)送密碼或者敏感信息呐赡,對于一些金融業(yè)務應用和聊天應用來說退客,應該是必備的特性。
- Notification Content Extension
我們可以利用Notification Content Extension這個擴展進行通知視圖的自定義链嘀。
NotificationViewController
繼承于普通的UIViewController
萌狂,我們可以自己定義各種UI。這里就是創(chuàng)建好之后怀泊,默認自帶的一個label茫藏。
注意:需要在info.plist文件里設置對應的
UNNotificationExtensionCategory
要不然,這個自定義的UI是不起作用的霹琼。
#import "NotificationViewController.h"
#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
@interface NotificationViewController () <UNNotificationContentExtension>
@property IBOutlet UILabel *label;
@end
@implementation NotificationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any required interface initialization here.
}
- (void)didReceiveNotification:(UNNotification *)notification {
self.label.text = @"這是自定義的label";//notification.request.content.body;
}
效果圖:
多媒體通知(通知中攜帶圖片务傲、音頻、視頻)
具體文件類型和大小限制如下:
- 為本地通知增加多媒體內容
//多媒體通知(發(fā)送一個圖片)
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"attachment_pic" ofType:@"jpg"];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment.identifier" URL:imageURL options:nil error:nil];
content.attachments = @[attachment,attachment];
效果:
可以添加音頻:
//多媒體通知(發(fā)送一個mp3)
NSString *mp3Path = [[NSBundle mainBundle] pathForResource:@"Fly" ofType:@"mp3"];
NSURL *mp3URL = [NSURL fileURLWithPath:mp3Path];
NSError *error = nil;
UNNotificationAttachment *mp3Attachment = [UNNotificationAttachment attachmentWithIdentifier:@"mp3.attachment.identifier" URL:mp3URL options:nil error:&error];
if (error) {
NSLog(@"error = %@", error);
}
content.attachments = @[mp3Attachment];
結果:
如果同時添加了圖片和mp3也就是添加多個附件的時候枣申,只會展示第一個售葡。
因為沒有合適的視頻,視頻我估計也就差不多忠藤,我這邊就不測試了挟伙。
- 為遠程通知增加多媒體內容
遠程通知攜帶多媒體內容需要借助上面提到的Notification Service Extension,在攔截到通知的時候模孩,可以是一個遠程的url尖阔,也可以是bundle中的一個url,在處理完之后榨咐,更改通知的attachment
就可以了介却。