iOS10之后的通知具有通知擴(kuò)展功能,可以在系統(tǒng)受到通知炫隶、展示通知時(shí)做一些事情淋叶。
- UNNotificationServiceExtension:通知服務(wù)擴(kuò)展,是在收到通知后伪阶,展示通知前煞檩,做一些事情的处嫌。比如,增加附件斟湃,網(wǎng)絡(luò)請(qǐng)求等锰霜。點(diǎn)擊查看官網(wǎng)文檔
- UNNotificationContentExtension:通知內(nèi)容擴(kuò)展,是在展示通知時(shí)展示一個(gè)自定義的用戶界面桐早。點(diǎn)擊查看官網(wǎng)文檔
1. 創(chuàng)建一個(gè)UNNotificationServiceExtension和UNNotificationContentExtension:
注意:
(1)如上圖默認(rèn)情況下癣缅,兩個(gè)新生成target的bundleId是主工程名字的bundleId.target名稱,不需要修改哄酝;
(2)target支持的系統(tǒng)版本號(hào)為10及以上友存。
2. 通知服務(wù)擴(kuò)展UNNotificationServiceExtension
在NotificationService.m文件中,有兩個(gè)自動(dòng)生成的方法:
// 系統(tǒng)接到通知后陶衅,有最多30秒在這里重寫通知內(nèi)容(如下載附件并更新通知)
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
// 處理過(guò)程超時(shí)屡立,則收到的通知直接展示出來(lái)
- (void)serviceExtensionTimeWillExpire;
代碼示例如下:
//
// NotificationService.m
// QiNotificationService
//
// Created by wangdacheng on 2018/9/29.
// Copyright ? 2018年 dac. All rights reserved.
//
#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];
// 注:為通知下拉手動(dòng)展開時(shí),可添加多個(gè)事件
// UNNotificationActionOptions包含三個(gè)值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.通知擴(kuò)展功能須在aps串中設(shè)置字段"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";
}
if ([type isEqualToString:@"video"]) {
ext = @"mp4";
}
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"}}
說(shuō)明:
(1)加載并處理附件的時(shí)間要在30秒之內(nèi)焚刺,才會(huì)達(dá)到預(yù)期效果;
(2)UNNotificationAttachment的url參數(shù)接收的是本地文件的url门烂;
(3)服務(wù)端在處理推送內(nèi)容時(shí)乳愉,需要加上文件類型字段;
(4)aps字符串中的mutable-content字段需要設(shè)置為1屯远;
(5)在對(duì)NotificationService這個(gè)target打斷點(diǎn)debug的時(shí)候蔓姚,需要在XCode頂欄選擇編譯運(yùn)行的target為NotificationService,否則無(wú)法進(jìn)行實(shí)時(shí)debug慨丐。
3. 通知內(nèi)容擴(kuò)展UNNotificationContentExtension
通知內(nèi)容擴(kuò)展過(guò)程中坡脐,展示在用戶面前的NotificationViewController的結(jié)構(gòu)說(shuō)明如圖:
(1)設(shè)置actions:從NotificationViewController這個(gè)類可以看出,它直接繼承于ViewController房揭,因此可以在這個(gè)類中重寫相關(guān)方法备闲,來(lái)修改界面的相關(guān)布局及樣式。在這個(gè)界面展開之前崩溪,用戶通過(guò)UNNotificationAction還是可以與相應(yīng)推送通知交互的浅役,但是用戶和這個(gè)通知內(nèi)容擴(kuò)展界面無(wú)法直接交互。(這些actions有兩種設(shè)置途徑:用戶可以通過(guò)在AppDelegate中實(shí)例化UIUserNotificationSettings來(lái)間接設(shè)置這些actions伶唯;在UNNotificationServiceExtension中也可以處理這些actions觉既。)
(2)設(shè)置category:推送通知內(nèi)容中的category字段,與UNNotificationContentExtension的info.plist中UNNotificationExtensionCategory字段的值要匹配到,系統(tǒng)才能找到自定義的UI瞪讼。在aps字符串中直接設(shè)置category字段如下:
{ "aps":{ "alert":"Testing...(0)","badge":1,"sound":"default","category":"QiShareCategoryIdentifier"}}
在NotificationService.m中設(shè)置category的值:
self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";
info.plist中關(guān)于category的配置:
(3)UNNotificationContentExtension協(xié)議:NotificationViewController 中生成時(shí)默認(rèn)實(shí)現(xiàn)了钧椰,簡(jiǎ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)容擴(kuò)展界面示例代碼如下:
//
// NotificationViewController.m
// NotificationContent
//
// Created by wangdacheng on 2018/9/29.
// Copyright ? 2018年 dac. All rights reserved.
//
#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);
// 設(shè)置控件邊框顏色
[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
說(shuō)明:
(1)宿主工程、服務(wù)擴(kuò)展target和內(nèi)容擴(kuò)展target三者中支持的系統(tǒng)版本號(hào)要一致希柿。
(2)自定義視圖的大小可以通過(guò)設(shè)置NotificationViewController的preferredContentSize大小來(lái)控制诊沪,但是用戶體驗(yàn)稍顯突兀,可以通過(guò)設(shè)置info.plist中的UNNotificationExtensionInitialContentSizeRatio屬性的值來(lái)優(yōu)化曾撤;
(3)contentExtension中的info.plist中NSExtension下的NSExtensionAttributes字段下可以配置以下屬性的值端姚,UNNotificationExtensionCategory:表示自定義內(nèi)容假面可以識(shí)別的category,可以為數(shù)組挤悉,即可以為這個(gè)content綁定多個(gè)通知渐裸;UNNotificationExtensionInitialContentSizeRatio:默認(rèn)的UI界面的高寬比;UNNotificationExtensionDefaultContentHidden:是否顯示系統(tǒng)默認(rèn)的標(biāo)題欄和內(nèi)容装悲,可選參數(shù)昏鹃;UNNotificationExtensionOverridesDefaultTitle:是否讓系統(tǒng)采用消息的標(biāo)題作為通知的標(biāo)題,可選參數(shù)诀诊。
(4)處理通知內(nèi)容擴(kuò)展的過(guò)程中關(guān)于identifier的設(shè)置共有五處(UNNotificationAction洞渤、UNNotificationCategory、bestAttemptContent畏梆、contentExtension中的info.plist中您宪,aps字符串中)奈懒,請(qǐng)區(qū)別不同identifier的作用奠涌。
(5)兩個(gè)擴(kuò)展聯(lián)合使用,在XCode中選擇當(dāng)前target磷杏,才能打斷點(diǎn)看到相應(yīng)log信息溜畅。