iOS 推送通知及推送擴展

概述

iOS中的通知包括本地推送通知遠程推送通知,二者在iOS系統中均可以經過彈出橫幅的形式來提醒用戶,點擊橫幅會打開應用。在iOS 10及以後版本的系統中,還支持通知擴展功能(UNNotificationServiceExtension、UNNotificationContentExtension),下面就來詳細介紹iOS推送通知的相關功能及操做。html


1、本地推送通知

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

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系統中回調方式有所差別,以下:git

  • 系統版本 < 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向用戶請求推送通知權限的提示彈窗: github

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

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

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

PS:json

  • 當用戶拒絕受權推送通知時,app沒法接收通知;(用戶能夠到設置->通知->相應app,手動設置通知選項)
  • 通知的聲音在代碼中指定,由系統播放,時長必須在30s內,不然將被默認聲音替換,而且自定義聲音文件必須放到main bundle中。
  • 本地通知有數量限制,超過必定數量(64個)將被系統忽略(數據來源於網絡,具體時間間隔待驗證)。



2、遠程推送通知

遠程推送通知是經過蘋果的APNsApple Push Notification service)發送到app,而APNs必須先知道用戶設備的令牌(device token)。在啓動時,appAPNs通訊並接收device token,而後將其轉發到App ServerApp Server將該令牌和要發送的通知消息發送至APNsPS:蘋果官網APNs概述數組

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

  • 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



3、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…

相關文章
相關標籤/搜索