iOS 通知擴(kuò)展

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:
創(chuàng)建兩個(gè)target

創(chuàng)建兩個(gè)target的結(jié)果

注意:
(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ō)明如圖:


通知內(nèi)容擴(kuò)展界面

(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的配置:


關(guān)于UNNotificationExtensionCategory的設(shè)置

(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信息溜畅。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市极祸,隨后出現(xiàn)的幾起案子慈格,更是在濱河造成了極大的恐慌,老刑警劉巖遥金,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浴捆,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡稿械,警方通過(guò)查閱死者的電腦和手機(jī)选泻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人页眯,你說(shuō)我怎么就攤上這事梯捕。” “怎么了窝撵?”我有些...
    開封第一講書人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵傀顾,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我碌奉,道長(zhǎng)短曾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任赐劣,我火速辦了婚禮错英,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘隆豹。我一直安慰自己椭岩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開白布璃赡。 她就那樣靜靜地躺著判哥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪碉考。 梳的紋絲不亂的頭發(fā)上塌计,一...
    開封第一講書人閱讀 49,031評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音侯谁,去河邊找鬼锌仅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛墙贱,可吹牛的內(nèi)容都是我干的热芹。 我是一名探鬼主播,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼惨撇,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼伊脓!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起魁衙,我...
    開封第一講書人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤报腔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后剖淀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纯蛾,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年纵隔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了翻诉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片帆卓。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖米丘,靈堂內(nèi)的尸體忽然破棺而出剑令,到底是詐尸還是另有隱情,我是刑警寧澤拄查,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布吁津,位于F島的核電站,受9級(jí)特大地震影響堕扶,放射性物質(zhì)發(fā)生泄漏碍脏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一稍算、第九天 我趴在偏房一處隱蔽的房頂上張望典尾。 院中可真熱鬧,春花似錦糊探、人聲如沸钾埂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)褥紫。三九已至,卻和暖如春瞪慧,著一層夾襖步出監(jiān)牢的瞬間髓考,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工弃酌, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留氨菇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓妓湘,卻偏偏與公主長(zhǎng)得像查蓉,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子多柑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 無(wú)論設(shè)備處于鎖定狀態(tài)還是使用中奶是,都可以使用通知提供及時(shí)、重要的信息竣灌。無(wú)論app處于foreground、backg...
    pro648閱讀 7,766評(píng)論 1 21
  • 點(diǎn)擊查看原文 Web SDK 開發(fā)手冊(cè) SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個(gè)完善的 IM 系統(tǒng)...
    layjoy閱讀 13,674評(píng)論 0 15
  • iOS 10 中以前雜亂的和通知相關(guān)的 API 都被統(tǒng)一了秆麸,現(xiàn)在開發(fā)者可以使用獨(dú)立的 UserNotificati...
    ios設(shè)計(jì)閱讀 762評(píng)論 0 2
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理初嘹,服務(wù)發(fā)現(xiàn),斷路器沮趣,智...
    卡卡羅2017閱讀 134,599評(píng)論 18 139
  • 喜歡這個(gè)造型~ 可惜畫的不好看~ 接下來(lái)每天畫一遍~ 死磕一周~ 看看是什么情況~ 堅(jiān)持?~
    梅子吉祥如意懷德閱讀 114評(píng)論 0 3