級別: ★★☆☆☆
標簽:「iOS通知擴展」「iOS推送擴展」「UNNotificationServiceExtension」「UNNotificationContentExtension」
作者: dac_1033
審校: QiShare團隊
iOS10之后的通知具有擴展功能菠隆,可以在系統(tǒng)收到通知、展示通知時做一些事情禁悠。下面是實現(xiàn)步驟要點介紹:
1. 創(chuàng)建UNNotificationServiceExtension和UNNotificationContentExtension:
- UNNotificationServiceExtension:通知服務擴展,是在收到通知后囊陡,展示通知前纵竖,做一些事情的。比如梆掸,增加附件瘪贱,網(wǎng)絡請求等纱控。點擊查看官網(wǎng)文檔
- UNNotificationContentExtension:通知內(nèi)容擴展,是在展示通知時展示一個自定義的用戶界面政敢。點擊查看官網(wǎng)文檔
注意:
- 如上圖默認情況下其徙,兩個新生成target的bundleId是主工程名字的bundleId.target名稱胚迫,不需要修改喷户;
- target支持的iOS版本為10.0及以上。
2. 通知服務擴展UNNotificationServiceExtension
在NotificationService.m文件中访锻,有兩個方法:
// 系統(tǒng)接到通知后褪尝,有最多30秒在這里重寫通知內(nèi)容(如下載附件并更新通知)
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
// 處理過程超時,則收到的通知直接展示出來
- (void)serviceExtensionTimeWillExpire;
代碼示例如下:
#import "NotificationService.h"
#import <AVFoundation/AVFoundation.h>
@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
//// Modify the notification content here...
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
// 注:為通知下拉手動展開時期犬,可添加多個事件
// UNNotificationActionOptions包含三個值UNNotificationActionOptionAuthenticationRequired河哑、UNNotificationActionOptionDestructive、UNNotificationActionOptionForeground
UNNotificationAction * actionA =[UNNotificationAction actionWithIdentifier:@"ActionA" title:@"Required" options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"ActionB" title:@"Destructive" options:UNNotificationActionOptionDestructive];
UNNotificationAction * actionC = [UNNotificationAction actionWithIdentifier:@"ActionC" title:@"Foreground" options:UNNotificationActionOptionForeground];
UNTextInputNotificationAction * actionD = [UNTextInputNotificationAction actionWithIdentifier:@"ActionD"
title:@"Input-Destructive"
options:UNNotificationActionOptionDestructive
textInputButtonTitle:@"Send"
textInputPlaceholder:@"input some words here ..."];
NSMutableArray *actionArr = [[NSMutableArray alloc] initWithObjects:actionA, actionB, actionC, actionD, nil];
if (actionArr.count) {
UNNotificationCategory * notficationCategory = [UNNotificationCategory categoryWithIdentifier:@"categoryNoOperationAction"
actions:actionArr
intentIdentifiers:@[@"ActionA",@"ActionB",@"ActionC",@"ActionD"]
options:UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:notficationCategory]];
}
// 注:1.通知擴展功能須在aps串中設置字段"mutable-content":1龟虎; 2.多媒體的字段可以與appServer協(xié)議制定璃谨;
self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";
NSDictionary *userInfo = self.bestAttemptContent.userInfo;
NSString *mediaUrl = [NSString stringWithFormat:@"%@", userInfo[@"media"][@"url"]];
if (!mediaUrl.length) {
self.contentHandler(self.bestAttemptContent);
}
else {
[self loadAttachmentForUrlString:mediaUrl withType:userInfo[@"media"][@"type"] completionHandle:^(UNNotificationAttachment *attach) {
if (attach) {
self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];
}
self.contentHandler(self.bestAttemptContent);
}];
}
}
- (void)loadAttachmentForUrlString:(NSString *)urlStr withType:(NSString *)type completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler {
__block UNNotificationAttachment *attachment = nil;
NSURL *attachmentURL = [NSURL URLWithString:urlStr];
NSString *fileExt = [self fileExtensionForMediaType:type];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session downloadTaskWithURL:attachmentURL completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
if (error != nil) {
NSLog(@"加載多媒體失敗 %@", error.localizedDescription);
}
else {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExt]];
[fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
// 自定義推送UI需要
NSMutableDictionary * dict = [self.bestAttemptContent.userInfo mutableCopy];
[dict setObject:[NSData dataWithContentsOfURL:localURL] forKey:@"image"];
self.bestAttemptContent.userInfo = dict;
NSError *attachmentError = nil;
attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
if (attachmentError) {
NSLog(@"%@", attachmentError.localizedDescription);
}
}
completionHandler(attachment);
}] resume];
}
- (NSString *)fileExtensionForMediaType:(NSString *)type {
NSString *ext = type;
if ([type isEqualToString:@"image"]) {
ext = @"jpg";
}
else if ([type isEqualToString:@"video"]) {
ext = @"mp4";
}
else if ([type isEqualToString:@"audio"]) {
ext = @"mp3";
}
return [@"." stringByAppendingString:ext];
}
- (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);
}
@end
aps串格式:
{"aps":{"alert":{"title":"Title...","subtitle":"Subtitle...","body":"Body..."},"sound":"default","badge": 1,"mutable-content": 1,"category": "realtime",},"msgid":"123","media":{"type":"image","url":"https://www.fotor.com/images2/features/photo_effects/e_bw.jpg"}}
說明:
- 加載并處理附件的時間要在30秒之內(nèi),才會達到預期效果鲤妥;
- UNNotificationAttachment的url參數(shù)接收的是本地文件的url佳吞;
- 服務端在處理推送內(nèi)容時,需要加上文件類型字段棉安;
- aps字符串中的mutable-content字段需要設置為1底扳;
- 在對NotificationService這個target打斷點debug的時候,需要在XCode頂欄選擇編譯運行的target為NotificationService贡耽,否則無法進行實時debug衷模。
3. 通知內(nèi)容擴展UNNotificationContentExtension
通知內(nèi)容擴展過程中鹊汛,展示在用戶面前的NotificationViewController的結(jié)構(gòu)說明如圖如下:
1、設置actions:
從NotificationViewController這個類可以看出阱冶,它直接繼承于ViewController刁憋,因此可以在這個類中重寫相關方法,來修改界面的相關布局及樣式熙揍。在這個界面展開之前职祷,用戶通過UNNotificationAction還是可以與相應推送通知交互的,但是用戶和這個通知內(nèi)容擴展界面無法直接交互届囚。(這些actions有兩種設置途徑:用戶可以通過在AppDelegate中實例化UIUserNotificationSettings來間接設置這些actions有梆;在UNNotificationServiceExtension中也可以處理這些actions。)
2意系、設置category:
推送通知內(nèi)容中的category字段泥耀,與UNNotificationContentExtension的info.plist中UNNotificationExtensionCategory字段的值要匹配到,系統(tǒng)才能找到自定義的UI蛔添。
在aps字符串中直接設置category字段如下:
{ "aps":{ "alert":"Testing...(0)","badge":1,"sound":"default","category":"QiShareCategoryIdentifier"}}
在NotificationService.m中設置category的值如下:
self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";
info.plist中關于category的配置如下:
3痰催、UNNotificationContentExtension協(xié)議:NotificationViewController 中生成時默認實現(xiàn)了。
簡單的英文注釋很明了:
// This will be called to send the notification to be displayed by
// the extension. If the extension is being displayed and more related
// notifications arrive (eg. more messages for the same conversation)
// the same method will be called for each new notification.
- (void)didReceiveNotification:(UNNotification *)notification迎瞧;
// If implemented, the method will be called when the user taps on one
// of the notification actions. The completion handler can be called
// after handling the action to dismiss the notification and forward the
// action to the app if necessary.
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion
// Called when the user taps the play or pause button.
- (void)mediaPlay;
- (void)mediaPause;
4夸溶、UNNotificationAttachment:attachment支持
- 音頻5M(kUTTypeWaveformAudio/kUTTypeMP3/kUTTypeMPEG4Audio/kUTTypeAudioInterchangeFileFormat)
- 圖片10M(kUTTypeJPEG/kUTTypeGIF/kUTTypePNG)
- 視頻50M(kUTTypeMPEG/kUTTypeMPEG2Video/kUTTypeMPEG4/kUTTypeAVIMovie)
自定義內(nèi)容擴展界面示例代碼如下:
#import "NotificationViewController.h"
#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
#define Margin 15
@interface NotificationViewController () <UNNotificationContentExtension>
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *subLabel;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *hintLabel;
@end
@implementation NotificationViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGPoint origin = self.view.frame.origin;
CGSize size = self.view.frame.size;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(Margin, Margin, size.width-Margin*2, 30)];
self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.label];
self.subLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.label.frame)+10, size.width-Margin*2, 30)];
self.subLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.subLabel];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.subLabel.frame)+10, 100, 100)];
[self.view addSubview:self.imageView];
self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.imageView.frame)+10, size.width-Margin*2, 20)];
[self.hintLabel setText:@"我是hintLabel"];
[self.hintLabel setFont:[UIFont systemFontOfSize:14]];
[self.hintLabel setTextAlignment:NSTextAlignmentLeft];
[self.view addSubview:self.hintLabel];
self.view.frame = CGRectMake(origin.x, origin.y, size.width, CGRectGetMaxY(self.imageView.frame)+Margin);
// 設置控件邊框顏色
[self.label.layer setBorderColor:[UIColor redColor].CGColor];
[self.label.layer setBorderWidth:1.0];
[self.subLabel.layer setBorderColor:[UIColor greenColor].CGColor];
[self.subLabel.layer setBorderWidth:1.0];
[self.imageView.layer setBorderWidth:2.0];
[self.imageView.layer setBorderColor:[UIColor blueColor].CGColor];
[self.view.layer setBorderWidth:2.0];
[self.view.layer setBorderColor:[UIColor cyanColor].CGColor];
}
- (void)didReceiveNotification:(UNNotification *)notification {
self.label.text = notification.request.content.title;
self.subLabel.text = [NSString stringWithFormat:@"%@ [ContentExtension modified]", notification.request.content.subtitle];
NSData *data = notification.request.content.userInfo[@"image"];
UIImage *image = [UIImage imageWithData:data];
[self.imageView setImage:image];
}
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion {
[self.hintLabel setText:[NSString stringWithFormat:@"觸發(fā)了%@", response.actionIdentifier]];
if ([response.actionIdentifier isEqualToString:@"ActionA"]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
completion(UNNotificationContentExtensionResponseOptionDismiss);
});
} else if ([response.actionIdentifier isEqualToString:@"ActionB"]) {
} else if ([response.actionIdentifier isEqualToString:@"ActionC"]) {
} else if ([response.actionIdentifier isEqualToString:@"ActionD"]) {
} else {
completion(UNNotificationContentExtensionResponseOptionDismiss);
}
completion(UNNotificationContentExtensionResponseOptionDoNotDismiss);
}
@end
說明:
- 服務擴展target和內(nèi)容擴展target在配置中所支持的系統(tǒng)版本要在iOS10及以上;
- 自定義視圖的大小可以通過設置NotificationViewController的preferredContentSize大小來控制凶硅,但是用戶體驗稍顯突兀缝裁,可以通過設置info.plist中的UNNotificationExtensionInitialContentSizeRatio屬性的值來優(yōu)化;
- contentExtension中的info.plist中NSExtension下的NSExtensionAttributes字段下可以配置以下屬性的值足绅,UNNotificationExtensionCategory:表示自定義內(nèi)容假面可以識別的category捷绑,可以為數(shù)組,即可以為這個content綁定多個通知氢妈;UNNotificationExtensionInitialContentSizeRatio:默認的UI界面的高寬比粹污;UNNotificationExtensionDefaultContentHidden:是否顯示系統(tǒng)默認的標題欄和內(nèi)容,可選參數(shù)首量;UNNotificationExtensionOverridesDefaultTitle:是否讓系統(tǒng)采用消息的標題作為通知的標題壮吩,可選參數(shù)。
- 處理通知內(nèi)容擴展的過程中關于identifier的設置共有五處(UNNotificationAction加缘、UNNotificationCategory鸭叙、bestAttemptContent、contentExtension中的info.plist中生百,aps字符串中)递雀,請區(qū)別不同identifier的作用。
- 兩個擴展聯(lián)合使用蚀浆,在XCode中選擇當前target缀程,才能打斷點看到相應log信息搜吧。
工程源碼:GitHub地址