iOS 通知擴充套件

QiShare發表於2018-10-08

級別: ★★☆☆☆
標籤:「iOS通知擴充套件」「iOS推送擴充套件」「UNNotificationServiceExtension」「UNNotificationContentExtension」
作者: dac_1033
審校: QiShare團隊


iOS10之後的通知具有擴充套件功能,可以在系統收到通知、展示通知時做一些事情。下面是實現步驟要點介紹:

1. 建立UNNotificationServiceExtension和UNNotificationContentExtension:

  • UNNotificationServiceExtension:通知服務擴充套件,是在收到通知後,展示通知前,做一些事情的。比如,增加附件,網路請求等。點選檢視官網文件
  • UNNotificationContentExtension:通知內容擴充套件,是在展示通知時展示一個自定義的使用者介面。點選檢視官網文件

建立兩個target
建立兩個target的結果
注意:

  • 如上圖預設情況下,兩個新生成target的bundleId是主工程名字的bundleId.target名稱,不需要修改;
  • target支援的iOS版本為10.0及以上。

2. 通知服務擴充套件UNNotificationServiceExtension

在NotificationService.m檔案中,有兩個方法:

// 系統接到通知後,有最多30秒在這裡重寫通知內容(如下載附件並更新通知)
- (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協議制定;
    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":"www.fotor.com/images2/fea…"}}

說明:

  • 載入並處理附件的時間要在30秒之內,才會達到預期效果;
  • UNNotificationAttachment的url引數接收的是本地檔案的url;
  • 服務端在處理推送內容時,需要加上檔案型別欄位;
  • aps字串中的mutable-content欄位需要設定為1;
  • 在對NotificationService這個target打斷點debug的時候,需要在XCode頂欄選擇編譯執行的target為NotificationService,否則無法進行實時debug。

3. 通知內容擴充套件UNNotificationContentExtension

通知內容擴充套件過程中,展示在使用者面前的NotificationViewController的結構說明如圖如下:

通知內容擴充套件介面

1、設定actions: 從NotificationViewController這個類可以看出,它直接繼承於ViewController,因此可以在這個類中重寫相關方法,來修改介面的相關佈局及樣式。在這個介面展開之前,使用者通過UNNotificationAction還是可以與相應推送通知互動的,但是***使用者和這個通知內容擴充套件介面無法直接互動***。(這些actions有兩種設定途徑:使用者可以通過在AppDelegate中例項化UIUserNotificationSettings來間接設定這些actions;在UNNotificationServiceExtension中也可以處理這些actions。)

2、設定category: 推送通知內容中的category欄位,與UNNotificationContentExtension的info.plist中UNNotificationExtensionCategory欄位的值要匹配到,系統才能找到自定義的UI。

在aps字串中直接設定category欄位如下:

{ "aps":{ "alert":"Testing...(0)","badge":1,"sound":"default","category":"QiShareCategoryIdentifier"}}
複製程式碼

在NotificationService.m中設定category的值如下:

self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";
複製程式碼

info.plist中關於category的配置如下:

關於UNNotificationExtensionCategory的設定

3、UNNotificationContentExtension協議:NotificationViewController 中生成時預設實現了。

簡單的英文註釋很明瞭:

// 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)

自定義內容擴充套件介面示例程式碼如下:

#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:@"觸發了%@", 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和內容擴充套件target三者中支援的系統版本號要一致。
  • 自定義檢視的大小可以通過設定NotificationViewController的preferredContentSize大小來控制,但是使用者體驗稍顯突兀,可以通過設定info.plist中的UNNotificationExtensionInitialContentSizeRatio屬性的值來優化;
  • contentExtension中的info.plist中NSExtension下的NSExtensionAttributes欄位下可以配置以下屬性的值,UNNotificationExtensionCategory:表示自定義內容假面可以識別的category,可以為陣列,即可以為這個content繫結多個通知;UNNotificationExtensionInitialContentSizeRatio:預設的UI介面的高寬比;UNNotificationExtensionDefaultContentHidden:是否顯示系統預設的標題欄和內容,可選引數;UNNotificationExtensionOverridesDefaultTitle:是否讓系統採用訊息的標題作為通知的標題,可選引數。
  • 處理通知內容擴充套件的過程中關於identifier的設定共有五處(UNNotificationAction、UNNotificationCategory、bestAttemptContent、contentExtension中的info.plist中,aps字串中),請區別不同identifier的作用。
  • 兩個擴充套件聯合使用,在XCode中選擇當前target,才能打斷點看到相應log資訊。

工程原始碼:GitHub地址

關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

推薦文章:
iOS 本地通知
iOS 遠端通知

相關文章