iOS 推送通知及推送擴充套件

QiShare發表於2018-10-19

概述

iOS中的通知包括本地推送通知遠端推送通知,兩者在iOS系統中都可以通過彈出橫幅的形式來提醒使用者,點選橫幅會開啟應用。在iOS 10及之後版本的系統中,還支援通知擴充套件功能(UNNotificationServiceExtension、UNNotificationContentExtension),下面就來詳細介紹iOS推送通知的相關功能及操作。


一、本地推送通知

本地推送通知是由本地應用觸發的,是基於時間的通知形式,一般用於鬧鐘定時、待辦事項等提醒功能。傳送本地推送通知的大體步驟如下:
(1)註冊本地通知;
(2)建立本地通知相關變數,並初始化;
(3)設定處理通知的時間fireDate
(4)設定通知的內容:通知標題、通知聲音、圖示數字等;
(5)設定通知傳遞的引數userInfo,該字典內容可自定義(可選);
(6)新增這個本地通知到UNUserNotificationCenter

1. 註冊本地推送通知
- (void)sendLocalNotification {
    
    NSString *title = @"通知-title";
    NSString *subtitle = @"通知-subtitle";
    NSString *body = @"通知-body";
    NSInteger badge = 1;
    NSInteger timeInteval = 5;
    NSDictionary *userInfo = @{@"id": @"LOCAL_NOTIFY_SCHEDULE_ID"};
    
    if (@available(iOS 10.0, *)) {
        // 1.建立通知內容
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
        content.sound = [UNNotificationSound defaultSound];
        content.title = title;
        content.subtitle = subtitle;
        content.body = body;
        content.badge = @(badge);

        content.userInfo = userInfo;

        // 2.設定通知附件內容
        NSError *error = nil;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"logo_img_02@2x" ofType:@"png"];
        UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
        if (error) {
            NSLog(@"attachment error %@", error);
        }
        content.attachments = @[att];
        content.launchImageName = @"icon_certification_status1@2x";

        // 3.設定聲音
        UNNotificationSound *sound = [UNNotificationSound soundNamed:@"sound01.wav"];// [UNNotificationSound defaultSound];
        content.sound = sound;

        // 4.觸發模式
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInteval repeats:NO];

        // 5.設定UNNotificationRequest
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:LocalNotiReqIdentifer content:content trigger:trigger];

        // 6.把通知加到UNUserNotificationCenter, 到指定觸發點會被觸發
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        }];

    } else {
    
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        
        // 1.設定觸發時間(如果要立即觸發,無需設定)
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
        
        // 2.設定通知標題
        localNotification.alertBody = title;
        
        // 3.設定通知動作按鈕的標題
        localNotification.alertAction = @"檢視";
        
        // 4.設定提醒的聲音
        localNotification.soundName = @"sound01.wav";// UILocalNotificationDefaultSoundName;
        
        // 5.設定通知的 傳遞的userInfo
        localNotification.userInfo = userInfo;
        
        // 6.在規定的日期觸發通知
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

        // 7.立即觸發一個通知
        //[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }
}
複製程式碼
2. 取消本地推送通知
- (void)cancelLocalNotificaitons {
    
    // 取消一個特定的通知
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    // 獲取當前所有的本地通知
    if (!notificaitons || notificaitons.count <= 0) { return; }
    for (UILocalNotification *notify in notificaitons) {
        if ([[notify.userInfo objectForKey:@"id"] isEqualToString:@"LOCAL_NOTIFY_SCHEDULE_ID"]) {
            if (@available(iOS 10.0, *)) {
                [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[LocalNotiReqIdentifer]];
            } else {
                [[UIApplication sharedApplication] cancelLocalNotification:notify];
            }
            break;
        }
    }
    // 取消所有的本地通知
    //[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
複製程式碼
3. AppDelegate中的回撥方法

在上面的程式碼中我們設定了userInfo,在iOS中收到並點選通知,則會自動開啟應用。但是在不同版本的iOS系統中回撥方式有所差異,如下:

  • 系統版本 < iOS 10
// 如果App已經完全退出:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

// 當App已經完全退出時,獲取userInfo引數過程如下:
// NSDictionary *userInfoLocal = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// NSDictionary *userInfoRemote = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

// 如果App還在執行(前臺or後臺)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
複製程式碼
  • 系統版本 >= iOS 10
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
#endif
複製程式碼
4. 實現效果
  • app向使用者請求推送通知許可權的提示彈窗:

    請求推送通知許可權的提示彈窗

  • app處於不同狀態(前臺、後臺、鎖屏)時彈出通知的效果:

    app處於前臺
    app處於後臺
    鎖屏

PS:

  • 當使用者拒絕授權推送通知時,app無法接收通知;(使用者可以到設定->通知->相應app,手動設定通知選項)
  • 通知的聲音在程式碼中指定,由系統播放,時長必須在30s內,否則將被預設聲音替換,並且自定義聲音檔案必須放到main bundle中。
  • 本地通知有數量限制,超過一定數量(64個)將被系統忽略(資料來源於網路,具體時間間隔待驗證)。



二、遠端推送通知

遠端推送通知是通過蘋果的APNsApple Push Notification service)傳送到app,而APNs必須先知道使用者裝置的令牌(device token)。在啟動時,appAPNs通訊並接收device token,然後將其轉發到App ServerApp Server將該令牌和要傳送的通知訊息傳送至APNsPS:蘋果官網APNs概述

遠端推送通知的傳遞過程涉及幾個關鍵元件:

  • App Server
  • Apple推送通知服務(APNs)
  • 使用者的裝置(iPhone、iPad、iTouch、Mac等)
  • 相應的app

蘋果官方提供的遠端推送通知的傳遞示意圖如下:

遠端推送通知的傳遞過程

各關鍵元件之間的互動細節:

各關鍵元件之間的互動細節

  • 開發遠端推送功能首先要設定正確的推送證書和許可權,步驟如下:
    1)根據工程的Bundle Identifier,在蘋果開發者平臺中建立同名App ID,並勾選Push Notifications服務;
    2)在工程的“Capabilities”中設定Push NotificationsON
    3)遠端推送必須使用真機除錯,因為模擬器無法獲取得到device token

  • 在設定好證書和許可權後,按照以下步驟開發遠端推送功能:

1. 註冊遠端通知
// iOS 8及以上版本的遠端推送通知註冊
- (void)registerRemoteNotifications
{
    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
            if (!error) {
                NSLog(@"request authorization succeeded!");
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            } else {
                NSLog(@"request authorization failed!");
            }
        }];
    } else {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}
複製程式碼
2. App獲取device token
  • 在註冊遠端通知後,獲取device token的回撥方法:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
複製程式碼
  • 獲取device token失敗的回撥方法:
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
複製程式碼
3. app將device token傳送給App Server

只有蘋果公司知道device token的生成演算法,保證唯一,device token在app解除安裝後重灌等情況時會變化,因此為確保device token變化後app仍然能夠正常接收伺服器端傳送的通知,建議每次啟動應用都將獲取到的device token傳給App Server

4. App Server將device token和要推送的訊息傳送給APNs

將指定的device token和訊息內容傳送給APNs時,必須按照蘋果官方的訊息格式組織訊息內容。 PS:遠端通知訊息的欄位建立遠端通知訊息

訊息格式: {"aps":{"alert":{"title":"通知的title","subtitle":"通知的subtitle","body":"通知的body","title-loc-key":"TITLE_LOC_KEY","title-loc-args":["t_01","t_02"],"loc-key":"LOC_KEY","loc-args":["l_01","l_02"]},"sound":"sound01.wav","badge":1,"mutable-content":1,"category": "realtime"},"msgid":"123"}

5. APNs根據device token查詢相應裝置,並推送訊息

一般情況APNs可以根據deviceToken將訊息成功推送到相應裝置中,但也存在使用者解除安裝程式等導致推送訊息失敗的情況,這時App Server會收到APNs返回的錯誤資訊)。

6. AppDelegate中的回撥方法
// iOS<10時,且app被完全殺死
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

// 注:iOS10以上如果不使用UNUserNotificationCenter時,也將走此回撥方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

// 支援iOS7及以上系統
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;

//  iOS>=10: app在前臺獲取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;

//  iOS>=10: 點選通知進入app時觸發(殺死/切到後臺喚起)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler;
複製程式碼

在AppDelegate中註冊遠端推送通知並解析通知資料的完整程式碼如下:

#import "AppDelegate.h"
#import "ViewController.h"

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif

@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    ViewController *controller = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [_window setRootViewController:nav];
    [_window makeKeyAndVisible];
    
    ////註冊本地推送通知(具體操作在ViewController中)
    //[self registerLocalNotification];
    // 註冊遠端推送通知
    [self registerRemoteNotifications];
    
    return YES;
}

- (void)registerLocalNotification {

    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"request authorization succeeded!");
            }
        }];
    } else {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
}


- (void)registerRemoteNotifications
{
    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
            if (!error) {
                NSLog(@"request authorization succeeded!");
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            } else {
                NSLog(@"request authorization failed!");
            }
        }];
    } else {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}


- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    
    NSLog(@"didRegisterUserNotificationSettings");
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    NSLog(@"app收到本地推送(didReceiveLocalNotification:):%@", notification.userInfo);
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // 獲取並處理deviceToken
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"DeviceToken:%@\n", token);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@", error.description);
}

// 注:iOS10以上如果不使用UNUserNotificationCenter時,也將走此回撥方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // iOS6及以下系統
    if (userInfo) {
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {// app位於前臺通知
            NSLog(@"app位於前臺通知(didReceiveRemoteNotification:):%@", userInfo);
        } else {// 切到後臺喚起
            NSLog(@"app位於後臺通知(didReceiveRemoteNotification:):%@", userInfo);
        }
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler NS_AVAILABLE_IOS(7_0) {
    // iOS7及以上系統
    if (userInfo) {
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            NSLog(@"app位於前臺通知(didReceiveRemoteNotification:fetchCompletionHandler:):%@", userInfo);
        } else {
            NSLog(@"app位於後臺通知(didReceiveRemoteNotification:fetchCompletionHandler:):%@", userInfo);
        }
    }
    completionHandler(UIBackgroundFetchResultNewData);
}


#pragma mark - iOS>=10 中收到推送訊息

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
API_AVAILABLE(ios(10.0)){
    NSDictionary * userInfo = notification.request.content.userInfo;
    if (userInfo) {
        NSLog(@"app位於前臺通知(willPresentNotification:):%@", userInfo);
    }
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
API_AVAILABLE(ios(10.0)){
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if (userInfo) {
        NSLog(@"點選通知進入App時觸發(didReceiveNotificationResponse:):%@", userInfo);
    }
    completionHandler();
}

#endif


@end
複製程式碼
7. 使用Pusher工具模擬App Server推送通知

PusherSmartPush等工具一樣,是優秀的遠端推送測試工具,工具介面如下:

Pusher介面

  • Pusher的使用步驟說明:
    1)選擇p12格式的推送證書;
    2)設定是否為測試環境(預設勾選為測試環境,由於推送證書分為測試證書和生產證書,並且蘋果的APNs也分為測試和生產兩套環境,因此Pusher需要手動勾選推送環境);
    3)輸入device token
    4)輸入符合蘋果要求格式的aps字串;
    5)執行推送。

效果如下:

iOS遠端推送通知效果圖
點選橫幅開啟app,在回撥方法中獲取到的json串如下:

在App回撥方法中獲取的資料

PS:

  • 要使用遠端推送通知功能,需要至少啟動app一次;
  • 裝置不連網,是無法註冊遠端推送通知的;
  • 推送過程中aps串可在適當位置新增自定義欄位,訊息上限為4 KB



三、iOS 通知擴充套件

iOS 10及之後的推送通知具有擴充套件功能,包括兩個方面:

  • 通知服務擴充套件(UNNotificationServiceExtension),是在收到通知後且展示通知前允許開發者做一些事情,比如新增附件、載入網路請求等。點選檢視官網文件
  • 通知內容擴充套件(UNNotificationContentExtension),是在展示通知時展示一個自定義的使用者介面。點選檢視官網文件
1. 建立UNNotificationServiceExtension和UNNotificationContentExtension:

建立兩個target
建立兩個target的結果

注意:

  • target支援的iOS版本為10.0及以上,且當前系統支援target版本。
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:@"%@ [ServiceExtension modified]", self.bestAttemptContent.title];
    
    // 設定UNNotificationAction
    UNNotificationAction * actionA  =[UNNotificationAction actionWithIdentifier:@"ActionA" title:@"A_Required" options:UNNotificationActionOptionAuthenticationRequired];
    UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"ActionB" title:@"B_Destructive" options:UNNotificationActionOptionDestructive];
    UNNotificationAction * actionC = [UNNotificationAction actionWithIdentifier:@"ActionC" title:@"C_Foreground" options:UNNotificationActionOptionForeground];
    UNTextInputNotificationAction * actionD = [UNTextInputNotificationAction actionWithIdentifier:@"ActionD"
                                                                                            title:@"D_InputDestructive"
                                                                                          options:UNNotificationActionOptionDestructive
                                                                             textInputButtonTitle:@"Send"
                                                                             textInputPlaceholder:@"input some words here ..."];
    NSArray *actionArr = [[NSArray alloc] initWithObjects:actionA, actionB, actionC, actionD, nil];
    NSArray *identifierArr = [[NSArray alloc] initWithObjects:@"ActionA", @"ActionB", @"ActionC", @"ActionD", nil];
    UNNotificationCategory * notficationCategory = [UNNotificationCategory categoryWithIdentifier:@"QiShareCategoryIdentifier"
                                                                                          actions:actionArr
                                                                                intentIdentifiers:identifierArr
                                                                                          options:UNNotificationCategoryOptionCustomDismissAction];
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:notficationCategory]];
    
    // 設定categoryIdentifier
    self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";
    
    // 載入網路請求
    NSDictionary *userInfo =  self.bestAttemptContent.userInfo;
    NSString *mediaUrl = userInfo[@"media"][@"url"];
    NSString *mediaType = userInfo[@"media"][@"type"];
    if (!mediaUrl.length) {
        self.contentHandler(self.bestAttemptContent);
    } else {
        [self loadAttachmentForUrlString:mediaUrl withType:mediaType 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 getfileExtWithMediaType:type];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session downloadTaskWithURL:attachmentURL completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
        if (error) {
            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:@"QiShareCategoryIdentifier" URL:localURL options:nil error:&attachmentError];
            if (attachmentError) {
                NSLog(@"%@", attachmentError.localizedDescription);
            }
        }
        completionHandler(attachment);
    }] resume];
}

- (NSString *)getfileExtWithMediaType:(NSString *)mediaType {
    NSString *fileExt = mediaType;
    if ([mediaType isEqualToString:@"image"]) {
        fileExt = @"jpg";
    }
    if ([mediaType isEqualToString:@"video"]) {
        fileExt = @"mp4";
    }
    if ([mediaType isEqualToString:@"audio"]) {
        fileExt = @"mp3";
    }
    return [@"." stringByAppendingString:fileExt];
}

- (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":{"alert":{"title":"Title...","subtitle":"Subtitle...","body":"Body..."},"sound":"default","badge": 1,"mutable-content": 1,"category": "QiShareCategoryIdentifier",},"msgid":"123","media":{"type":"image","url":"www.fotor.com/images2/fea…"}}

PS:

  • 載入並處理附件時間上限為30秒,否則,通知按系統預設形式彈出;
  • UNNotificationAttachment的url接收的是本地檔案的url;
  • 服務端在處理推送內容時,最好加上媒體型別欄位;
  • aps字串中的mutable-content欄位需要設定為1;
  • 在對NotificationService進行debug時,需要在Xcode頂欄選擇編譯執行的target為NotificationService,否則無法進行實時debug。
3. 通知內容擴充套件UNNotificationContentExtension

通知內容擴充套件介面NotificationViewController的結構如下:

通知內容擴充套件介面

  • 設定actions: 從NotificationViewController直接繼承於ViewController,因此可以在這個類中重寫相關方法,來修改介面的相關佈局及樣式。在這個介面展開之前,使用者可以通過UNNotificationAction與相應推送通知互動,但是使用者和這個通知內容擴充套件介面無法直接互動。
  • 設定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的設定

  • 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;
複製程式碼
  • UNNotificationAttachment:attachment支援
    1)音訊5M(kUTTypeWaveformAudio/kUTTypeMP3/kUTTypeMPEG4Audio/kUTTypeAudioInterchangeFileFormat)
    2)圖片10M(kUTTypeJPEG/kUTTypeGIF/kUTTypePNG)
    3)視訊50M(kUTTypeMPEG/kUTTypeMPEG2Video/kUTTypeMPEG4/kUTTypeAVIMovie)
4. 自定義內容擴充套件介面與內容擴充套件功能聯合使用時,程式碼如下:
#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
複製程式碼

手機收到通知時的展示(aps串以上面第2點中提到的“訊息內容格式”為例)

推送擴充套件.gif

說明:

  • 服務擴充套件target和內容擴充套件target在配置中所支援的系統版本要在iOS10及以上;
  • 自定義檢視的大小可以通過設定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.com/QiShare/QiN…

相關文章