iOS 推送通知及通知扩展

1,563 阅读18分钟
原文链接: mp.weixin.qq.com

奇技指南

iOS中的通知包括本地推送通知和远程推送通知,下面就来详细介绍iOS推送通知的相关功能及操作。

本文来自360奇舞团Qishare团队投稿。

概述

iOS中的通知包括本地推送通知远程推送通知,两者在iOS系统中都可以通过弹出横幅的形式来提醒用户,点击横幅会打开应用。在iOS 10及之后版本的系统中,还支持通知扩展功能( UNNotificationServiceExtension 、UNNotificationContentExtension),下面就来详细介绍iOS推送通知的相关功能及操作。

本地推送通知

本地推送通知是由本地应用触发的,是基于时间的通知形式,一般用于闹钟定时、待办事项等提醒功能。发送本地推送通知的大体步骤如下: (1)注册本地通知; (2)创建本地通知相关变量,并初始化; (3)设置处理通知的时间 fireDate; (4)设置通知的内容:通知标题、通知声音、图标数字等; (5)设置通知传递的参数 userInfo,该字典内容可自定义(可选); (6)添加这个本地通知到 UNUserNotificationCenter

01

注册本地推送通知

  1. - (void)sendLocalNotification {

  2.    NSString *title = @"通知-title";

  3.    NSString *sutitle = @"通知-subtitle";

  4.    NSString *body = @"通知-body";

  5.    NSInteger badge = 1;

  6.    NSInteger timeInteval = 5;

  7.    NSDictionary *userInfo = @{@"id": @"LOCAL_NOTIFY_SCHEDULE_ID"};

  8.    if (@available(iOS 10.0, *)) {

  9.        // 1.创建通知内容

  10.        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

  11.        [content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];

  12.        content.sound = [UNNotificationSound defaultSound];

  13.        content.title = title;

  14.        content.subtitle = subtitle;

  15.        content.body = body;

  16.        content.badge = @(badge);

  17.        content.userInfo = userInfo;

  18.        // 2.设置通知附件内容

  19.        NSError *error = nil;

  20.        NSString *path = [[NSBundle mainBundle] pathForResource:@"logo_img_02@2x" ofType:@"png"];

  21.        UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];

  22.        if (error) {

  23.            NSLog(@"attachment error %@", error);

  24.        }

  25.        content.attachments = @[att];

  26.        content.launchImageName = @"icon_certification_status1@2x";

  27.        // 3.设置声音

  28.        UNNotificationSound *sound = [UNNotificationSound soundNamed:@"sound01.wav"];// [UNNotificationSound defaultSound];

  29.        content.sound = sound;

  30.        // 4.触发模式

  31.        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInteval repeats:NO];

  32.        // 5.设置UNNotificationRequest

  33.        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:LocalNotiReqIdentifer content:content trigger:trigger];

  34.        // 6.把通知加到UNUserNotificationCenter, 到指定触发点会被触发

  35.        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

  36.        }];

  37.    } else {

  38.        UILocalNotification *localNotification = [[UILocalNotification alloc] init];

  39.        // 1.设置触发时间(如果要立即触发,无需设置)

  40.        localNotification.timeZone = [NSTimeZone defaultTimeZone];

  41.        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

  42.        // 2.设置通知标题

  43.        localNotification.alertBody = title;

  44.        // 3.设置通知动作按钮的标题

  45.        localNotification.alertAction = @"查看";

  46.        // 4.设置提醒的声音

  47.        localNotification.soundName = @"sound01.wav";// UILocalNotificationDefaultSoundName;

  48.        // 5.设置通知的 传递的userInfo

  49.        localNotification.userInfo = userInfo;

  50.        // 6.在规定的日期触发通知

  51.        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

  52.        // 7.立即触发一个通知

  53.        //[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

  54.    }

  55. }

02

取消本地推送通知

  1. - (void)cancelLocalNotificaitons {

  2.    // 取消一个特定的通知

  3.    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];

  4.    // 获取当前所有的本地通知

  5.    if (!notificaitons || notificaitons.count <= 0) { return; }

  6.    for (UILocalNotification *notify in notificaitons) {

  7.        if ([[notify.userInfo objectForKey:@"id"] isEqualToString:@"LOCAL_NOTIFY_SCHEDULE_ID"]) {

  8.            if (@available(iOS 10.0, *)) {

  9.                [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[LocalNotiReqIdentifer]];

  10.            } else {

  11.                [[UIApplication sharedApplication] cancelLocalNotification:notify];

  12.            }

  13.            break;

  14.        }

  15.    }

  16.    // 取消所有的本地通知

  17.    //[[UIApplication sharedApplication] cancelAllLocalNotifications];

  18. }

03

AppDelegate中的回调方法

在上面的代码中我们设置了 userInfo,在iOS中收到并点击通知,则会自动打开应用。但是在不同版本的iOS系统中回调方式有所差异,如下:

  • 系统版本 < iOS 10

  1. // 如果App已经完全退出:

  2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

  3. // 当App已经完全退出时,获取userInfo参数过程如下:

  4. // NSDictionary *userInfoLocal = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

  5. // NSDictionary *userInfoRemote = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

  6. // 如果App还在运行(前台or后台)

  7. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

  • 系统版本 >= iOS 10

  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

  2. #pragma mark - UNUserNotificationCenterDelegate

  3. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

  4. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;

  5. #endif

04

实现效果

  • app向用户请求推送通知权限的提示弹窗:

  • app处于不同状态(前台、后台、锁屏)时弹出通知的效果:

PS:

  • 当用户拒绝授权推送通知时, app无法接收通知;(用户可以到设置->通知->相应  app,手动设置通知选项)

  • 通知的声音在代码中指定,由系统播放,时长必须在 30s内,否则将被默认声音替换,并且自定义声音文件必须放到  main bundle中。

  • 本地通知有数量限制,超过一定数量(64个)将被系统忽略(数据来源于网络,具体时间间隔待验证)。

远程推送通知

远程推送通知是通过苹果的APNsApplePushNotificationservice)发送到 app,而 APNs必须先知道用户设备的令牌( device token)。在启动时, appAPNs通信并接收 device token,然后将其转发到 AppServerAppServer将该令牌和要发送的通知消息发送至 APNs。 PS:苹果官网APNs概述

远程推送通知的传递过程涉及几个关键组件:

  • App Server

  • Apple推送通知服务(APNs)

  • 用户的设备(iPhone、iPad、iTouch、Mac等)

  • 相应的app

苹果官方提供的远程推送通知的传递示意图如下:

各关键组件之间的交互细节:

  • 开发远程推送功能首先要设置正确的推送证书和权限,步骤如下: 1)根据工程的 BundleIdentifier,在苹果开发者平台中创建同名 AppID,并勾选 PushNotifications服务; 2)在工程的“Capabilities”中设置 PushNotificationsON; 3)远程推送必须使用真机调试,因为模拟器无法获取得到 device token

  • 在设置好证书和权限后,按照以下步骤开发远程推送功能:

01

注册远程通知

    1. // iOS 8及以上版本的远程推送通知注册

    2. - (void)registerRemoteNotifications

    3. {

    4.    if (@available(iOS 10.0, *)) {

    5.        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    6.        center.delegate = self;

    7.        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {

    8.            if (!error) {

    9.                NSLog(@"request authorization succeeded!");

    10.                [[UIApplication sharedApplication] registerForRemoteNotifications];

    11.            } else {

    12.                NSLog(@"request authorization failed!");

    13.            }

    14.        }];

    15.    } else {

    16.        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);

    17.        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

    18.        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    19.        [[UIApplication sharedApplication] registerForRemoteNotifications];

    20.    }

    21. }

    02

    App获取device token

    • 在注册远程通知后,获取 device token的回调方法:

    1. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;

    • 获取 device token失败的回调方法:

    1. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

    03

    app将device token发送给App Server

    只有苹果公司知道 device token的生成算法,保证唯一, device token在app卸载后重装等情况时会变化,因此为确保 device token变化后app仍然能够正常接收服务器端发送的通知,建议每次启动应用都将获取到的 device token传给 AppServer

    04

     App Server将device token和要推送的消息发送给APNs

    将指定的 device token和消息内容发送给 APNs时,必须按照苹果官方的消息格式组织消息内容。 PS:远程通知消息的字段、创建远程通知消息

    消息格式: {"aps":{"alert":{"title":"通知的title","subtitle":"通知的subtitle","body":"通知的body","title-loc-key":"TITLELOCKEY","title-loc-args":["t01","t02"],"loc-key":"LOC KEY","loc-args":["l01","l_02"]},"sound":"sound01.wav","badge":1,"mutable-content":1,"category": "realtime"},"msgid":"123"}

    05

    APNs根据device token查找相应设备,并推送消息

    一般情况 APNs可以根据 deviceToken将消息成功推送到相应设备中,但也存在用户卸载程序等导致推送消息失败的情况,这时 AppServer会收到 APNs返回的错误信息)。

    06

    AppDelegate中的回调方法

    1. // iOS<10时,且app被完全杀死

    2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

    3. // 注:iOS10以上如果不使用UNUserNotificationCenter时,也将走此回调方法

    4. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

    5. // 支持iOS7及以上系统

    6. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;

    7. //  iOS>=10: app在前台获取到通知

    8. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;

    9. //  iOS>=10: 点击通知进入app时触发(杀死/切到后台唤起)

    10. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler;

    在AppDelegate中注册远程推送通知并解析通知数据的完整代码如下:

    1. #import "AppDelegate.h"

    2. #import "ViewController.h"

    3. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

    4. #import <UserNotifications/UserNotifications.h>

    5. #endif

    6. @interface AppDelegate () <UNUserNotificationCenterDelegate>

    7. @end

    8. @implementation AppDelegate

    9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    10.    ViewController *controller = [[ViewController alloc] init];

    11.    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];

    12.    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    13.    [_window setRootViewController:nav];

    14.    [_window makeKeyAndVisible];

    15.    ////注册本地推送通知(具体操作在ViewController中)

    16.    //[self registerLocalNotification];

    17.    // 注册远程推送通知

    18.    [self registerRemoteNotifications];

    19.    return YES;

    20. }

    21. - (void)registerLocalNotification {

    22.    if (@available(iOS 10.0, *)) {

    23.        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    24.        center.delegate = self;

    25.        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {

    26.            if (!error) {

    27.                NSLog(@"request authorization succeeded!");

    28.            }

    29.        }];

    30.    } else {

    31.        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

    32.        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    33.    }

    34. }

    35. - (void)registerRemoteNotifications

    36. {

    37.    if (@available(iOS 10.0, *)) {

    38.        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    39.        center.delegate = self;

    40.        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {

    41.            if (!error) {

    42.                NSLog(@"request authorization succeeded!");

    43.                [[UIApplication sharedApplication] registerForRemoteNotifications];

    44.            } else {

    45.                NSLog(@"request authorization failed!");

    46.            }

    47.        }];

    48.    } else {

    49.        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);

    50.        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

    51.        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    52.        [[UIApplication sharedApplication] registerForRemoteNotifications];

    53.    }

    54. }

    55. - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    56.    NSLog(@"didRegisterUserNotificationSettings");

    57. }

    58. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    59.    NSLog(@"app收到本地推送(didReceiveLocalNotification:):%@", notification.userInfo);

    60. }

    61. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    62.    // 获取并处理deviceToken

    63.    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

    64.    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

    65.    NSLog(@"DeviceToken:%@\n", token);

    66. }

    67. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    68.    NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@", error.description);

    69. }

    70. // 注:iOS10以上如果不使用UNUserNotificationCenter时,也将走此回调方法

    71. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    72.    // iOS6及以下系统

    73.    if (userInfo) {

    74.        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {// app位于前台通知

    75.            NSLog(@"app位于前台通知(didReceiveRemoteNotification:):%@", userInfo);

    76.        } else {// 切到后台唤起

    77.            NSLog(@"app位于后台通知(didReceiveRemoteNotification:):%@", userInfo);

    78.        }

    79.    }

    80. }

    81. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler NS_AVAILABLE_IOS(7_0) {

    82.    // iOS7及以上系统

    83.    if (userInfo) {

    84.        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {

    85.            NSLog(@"app位于前台通知(didReceiveRemoteNotification:fetchCompletionHandler:):%@", userInfo);

    86.        } else {

    87.            NSLog(@"app位于后台通知(didReceiveRemoteNotification:fetchCompletionHandler:):%@", userInfo);

    88.        }

    89.    }

    90.    completionHandler(UIBackgroundFetchResultNewData);

    91. }

    92. #pragma mark - iOS>=10 中收到推送消息

    93. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

    94. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

    95. API_AVAILABLE(ios(10.0)){

    96.    NSDictionary * userInfo = notification.request.content.userInfo;

    97.    if (userInfo) {

    98.        NSLog(@"app位于前台通知(willPresentNotification:):%@", userInfo);

    99.    }

    100.    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);

    101. }

    102. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler

    103. API_AVAILABLE(ios(10.0)){

    104.    NSDictionary * userInfo = response.notification.request.content.userInfo;

    105.    if (userInfo) {

    106.        NSLog(@"点击通知进入App时触发(didReceiveNotificationResponse:):%@", userInfo);

    107.    }

    108.    completionHandler();

    109. }

    110. #endif

    111. @end

    07

    使用Pusher工具模拟App Server推送通知

    PusherSmartPush等工具一样,是优秀的远程推送测试工具,工具界面如下:

    • Pusher的使用步骤说明:1)选择 p12格式的推送证书; 2)设置是否为测试环境(默认勾选为测试环境,由于推送证书分为测试证书和生产证书,并且苹果的 APNs也分为测试和生产两套环境,因此  Pusher需要手动勾选推送环境); 3)输入 device token; 4)输入符合苹果要求格式的 aps字符串; 5)执行推送。

    效果如下点击横幅打开app,在回调方法中获取到的 json串如下:

    PS:

    • 要使用远程推送通知功能,需要至少启动app一次;

    • 设备不连网,是无法注册远程推送通知的;

    • 推送过程中aps串可在适当位置添加自定义字段,消息上限为 4 KB

    iOS 通知扩展

    iOS 10及之后的推送通知具有扩展功能,包括两个方面:

    • 通知服务扩展(UNNotificationServiceExtension),是在收到通知后且展示通知前允许开发者做一些事情,比如添加附件、加载网络请求等。点击查看官网文档

    • 通知内容扩展(UNNotificationContentExtension),是在展示通知时展示一个自定义的用户界面。点击查看官网文档

    01

    创建UNNotificationServiceExtension和UNNotificationContentExtension:

    注意:

    • target支持的iOS版本为10.0及以上,且当前系统支持target版本。

    02

    通知服务扩展UNNotificationServiceExtension

    在NotificationService.m文件中,有两个回调方法:

    1. // 系统接到通知后,有最多30秒在这里重写通知内容(如下载附件并更新通知)

    2. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;

    3. // 处理过程超时,则收到的通知直接展示出来

    4. - (void)serviceExtensionTimeWillExpire;

    在通知服务扩展中加载网络请求,代码如下:

    1. #import "NotificationService.h"

    2. #import <AVFoundation/AVFoundation.h>

    3. @interface NotificationService ()

    4. @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);

    5. @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

    6. @end

    7. @implementation NotificationService

    8. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {

    9.    self.contentHandler = contentHandler;

    10.    self.bestAttemptContent = [request.content mutableCopy];

    11.    //// Modify the notification content here...

    12.    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [ServiceExtension modified]", self.bestAttemptContent.title];

    13.    // 设置UNNotificationAction

    14.    UNNotificationAction * actionA  =[UNNotificationAction actionWithIdentifier:@"ActionA" title:@"A_Required" options:UNNotificationActionOptionAuthenticationRequired];

    15.    UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"ActionB" title:@"B_Destructive" options:UNNotificationActionOptionDestructive];

    16.    UNNotificationAction * actionC = [UNNotificationAction actionWithIdentifier:@"ActionC" title:@"C_Foreground" options:UNNotificationActionOptionForeground];

    17.    UNTextInputNotificationAction * actionD = [UNTextInputNotificationAction actionWithIdentifier:@"ActionD"

    18.                                                                                            title:@"D_InputDestructive"

    19.                                                                                          options:UNNotificationActionOptionDestructive

    20.                                                                             textInputButtonTitle:@"Send"

    21.                                                                             textInputPlaceholder:@"input some words here ..."];

    22.    NSArray *actionArr = [[NSArray alloc] initWithObjects:actionA, actionB, actionC, actionD, nil];

    23.    NSArray *identifierArr = [[NSArray alloc] initWithObjects:@"ActionA", @"ActionB", @"ActionC", @"ActionD", nil];

    24.    UNNotificationCategory * notficationCategory = [UNNotificationCategory categoryWithIdentifier:@"QiShareCategoryIdentifier"

    25.                                                                                          actions:actionArr

    26.                                                                                intentIdentifiers:identifierArr

    27.                                                                                          options:UNNotificationCategoryOptionCustomDismissAction];

    28.    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:notficationCategory]];

    29.    // 设置categoryIdentifier

    30.    self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";

    31.    // 加载网络请求

    32.    NSDictionary *userInfo =  self.bestAttemptContent.userInfo;

    33.    NSString *mediaUrl = userInfo[@"media"][@"url"];

    34.    NSString *mediaType = userInfo[@"media"][@"type"];

    35.    if (!mediaUrl.length) {

    36.        self.contentHandler(self.bestAttemptContent);

    37.    } else {

    38.        [self loadAttachmentForUrlString:mediaUrl withType:mediaType completionHandle:^(UNNotificationAttachment *attach) {

    39.            if (attach) {

    40.                self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];

    41.            }

    42.            self.contentHandler(self.bestAttemptContent);

    43.        }];

    44.    }

    45. }

    46. - (void)loadAttachmentForUrlString:(NSString *)urlStr withType:(NSString *)type completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler

    47. {

    48.    __block UNNotificationAttachment *attachment = nil;

    49.    NSURL *attachmentURL = [NSURL URLWithString:urlStr];

    50.    NSString *fileExt = [self getfileExtWithMediaType:type];

    51.    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    52.    [[session downloadTaskWithURL:attachmentURL completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {

    53.        if (error) {

    54.            NSLog(@"加载多媒体失败 %@", error.localizedDescription);

    55.        } else {

    56.            NSFileManager *fileManager = [NSFileManager defaultManager];

    57.            NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExt]];

    58.            [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];

    59.            // 自定义推送UI需要

    60.            NSMutableDictionary * dict = [self.bestAttemptContent.userInfo mutableCopy];

    61.            [dict setObject:[NSData dataWithContentsOfURL:localURL] forKey:@"image"];

    62.            self.bestAttemptContent.userInfo = dict;

    63.            NSError *attachmentError = nil;

    64.            attachment = [UNNotificationAttachment attachmentWithIdentifier:@"QiShareCategoryIdentifier" URL:localURL options:nil error:&attachmentError];

    65.            if (attachmentError) {

    66.                NSLog(@"%@", attachmentError.localizedDescription);

    67.            }

    68.        }

    69.        completionHandler(attachment);

    70.    }] resume];

    71. }

    72. - (NSString *)getfileExtWithMediaType:(NSString *)mediaType {

    73.    NSString *fileExt = mediaType;

    74.    if ([mediaType isEqualToString:@"image"]) {

    75.        fileExt = @"jpg";

    76.    }

    77.    if ([mediaType isEqualToString:@"video"]) {

    78.        fileExt = @"mp4";

    79.    }

    80.    if ([mediaType isEqualToString:@"audio"]) {

    81.        fileExt = @"mp3";

    82.    }

    83.    return [@"." stringByAppendingString:fileExt];

    84. }

    85. - (void)serviceExtensionTimeWillExpire {

    86.    // Called just before the extension will be terminated by the system.

    87.    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.

    88.    self.contentHandler(self.bestAttemptContent);

    89. }

    90. @end

    消息内容格式: {"aps":{"alert":{"title":"Title...","subtitle":"Subtitle...","body":"Body..."},"sound":"default","badge": 1,"mutable-content": 1,"category": "QiShareCategoryIdentifier",},"msgid":"123","media":{"type":"image","url":"https://www.fotor.com/images2/features/photo effects/ebw.jpg"}}

    PS:

    • 加载并处理附件时间上限为30秒,否则,通知按系统默认形式弹出;

    • UNNotificationAttachment的url接收的是本地文件的url;

    • 服务端在处理推送内容时,最好加上媒体类型字段;

    • aps字符串中的mutable-content字段需要设置为1;

    • 在对NotificationService进行debug时,需要在Xcode顶栏选择编译运行的target为NotificationService,否则无法进行实时debug。

    03

    通知内容扩展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的值如下:

    1. self.bestAttemptContent.categoryIdentifier = @"QiShareCategoryIdentifier";

    info.plist中关于category的配置如下:

    • UNNotificationContentExtension协议:NotificationViewController 中生成时默认实现了。

    简单的英文注释很明了:

    1. // This will be called to send the notification to be displayed by

    2. // the extension. If the extension is being displayed and more related

    3. // notifications arrive (eg. more messages for the same conversation)

    4. // the same method will be called for each new notification.

    5. - (void)didReceiveNotification:(UNNotification *)notification;

    6. // If implemented, the method will be called when the user taps on one

    7. // of the notification actions. The completion handler can be called

    8. // after handling the action to dismiss the notification and forward the

    9. // action to the app if necessary.

    10. - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion

    11. // Called when the user taps the play or pause button.

    12. - (void)mediaPlay;

    13. - (void)mediaPause;

    • UNNotificationAttachment:attachment支持1)音频5M(kUTTypeWaveformAudio/kUTTypeMP3/kUTTypeMPEG4Audio/kUTTypeAudioInterchangeFileFormat)2)图片10M(kUTTypeJPEG/kUTTypeGIF/kUTTypePNG)3)视频50M(kUTTypeMPEG/kUTTypeMPEG2Video/kUTTypeMPEG4/kUTTypeAVIMovie)

    04

    自定义内容扩展界面与内容扩展功能联合使用时,代码如下:

    1. #import "NotificationViewController.h"

    2. #import <UserNotifications/UserNotifications.h>

    3. #import <UserNotificationsUI/UserNotificationsUI.h>

    4. #define Margin      15

    5. @interface NotificationViewController () <UNNotificationContentExtension>

    6. @property (nonatomic, strong) UILabel *label;

    7. @property (nonatomic, strong) UILabel *subLabel;

    8. @property (nonatomic, strong) UIImageView *imageView;

    9. @property (nonatomic, strong) UILabel *hintLabel;

    10. @end

    11. @implementation NotificationViewController

    12. - (void)viewDidLoad {

    13.    [super viewDidLoad];

    14.    CGPoint origin = self.view.frame.origin;

    15.    CGSize size = self.view.frame.size;

    16.    self.label = [[UILabel alloc] initWithFrame:CGRectMake(Margin, Margin, size.width-Margin*2, 30)];

    17.    self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    18.    [self.view addSubview:self.label];

    19.    self.subLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.label.frame)+10, size.width-Margin*2, 30)];

    20.    self.subLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    21.    [self.view addSubview:self.subLabel];

    22.    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.subLabel.frame)+10, 100, 100)];

    23.    [self.view addSubview:self.imageView];

    24.    self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(Margin, CGRectGetMaxY(self.imageView.frame)+10, size.width-Margin*2, 20)];

    25.    [self.hintLabel setText:@"我是hintLabel"];

    26.    [self.hintLabel setFont:[UIFont systemFontOfSize:14]];

    27.    [self.hintLabel setTextAlignment:NSTextAlignmentLeft];

    28.    [self.view addSubview:self.hintLabel];

    29.    self.view.frame = CGRectMake(origin.x, origin.y, size.width, CGRectGetMaxY(self.imageView.frame)+Margin);

    30.    // 设置控件边框颜色

    31.    [self.label.layer setBorderColor:[UIColor redColor].CGColor];

    32.    [self.label.layer setBorderWidth:1.0];

    33.    [self.subLabel.layer setBorderColor:[UIColor greenColor].CGColor];

    34.    [self.subLabel.layer setBorderWidth:1.0];

    35.    [self.imageView.layer setBorderWidth:2.0];

    36.    [self.imageView.layer setBorderColor:[UIColor blueColor].CGColor];

    37.    [self.view.layer setBorderWidth:2.0];

    38.    [self.view.layer setBorderColor:[UIColor cyanColor].CGColor];

    39. }

    40. - (void)didReceiveNotification:(UNNotification *)notification {

    41.    self.label.text = notification.request.content.title;

    42.    self.subLabel.text = [NSString stringWithFormat:@"%@ [ContentExtension modified]", notification.request.content.subtitle];

    43.    NSData *data = notification.request.content.userInfo[@"image"];

    44.    UIImage *image = [UIImage imageWithData:data];

    45.    [self.imageView setImage:image];

    46. }

    47. - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion {

    48.    [self.hintLabel setText:[NSString stringWithFormat:@"触发了%@", response.actionIdentifier]];

    49.    if ([response.actionIdentifier isEqualToString:@"ActionA"]) {

    50.        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    51.            completion(UNNotificationContentExtensionResponseOptionDismiss);

    52.        });

    53.    } else if ([response.actionIdentifier isEqualToString:@"ActionB"]) {

    54.    } else if ([response.actionIdentifier isEqualToString:@"ActionC"]) {

    55.    }  else if ([response.actionIdentifier isEqualToString:@"ActionD"]) {

    56.    } else {

    57.        completion(UNNotificationContentExtensionResponseOptionDismiss);

    58.    }

    59.    completion(UNNotificationContentExtensionResponseOptionDoNotDismiss);

    60. }

    61. @end

    手机收到通知时的展示(aps串以上面第2点中提到的“消息内容格式”为例)

    说明:

    • 服务扩展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信息。

    工程源码:

    https://github.com/QiShare/QiNotification

    界世的你当不

    只作你的肩膀

     360官方技术公众号 

    技术干货|一手资讯|精彩活动

    空·