UNNotificationServiceExtension简介

  • UNNotificationServiceExtension是iOS10推出后的一个新特性。

ios 远程推送p12证书_ios 远程推送p12证书

  • 从这张图上,我们可以看到,原先直接从APNs推送到用户手机的消息,中间添加了ServiceExtension处理的一个步骤(当然你也可以不处理),通过这个ServiceExtension,我们可以把即将给用户展示的通知内容,做各种自定义的处理,最终,给用户呈现一个更为丰富的通知,当然,如果网络不好,或者附件过大,可能在给定的时间内,我们没能将通知重新编辑,那么,则会显示发过来的原版通知内容。
  • ServiceExtension可以做什么呢?它的意义是什么呢?大致有以下几点
    1、 安全
    安全总是摆在第一位的,从iOS9开始,苹果鼓励我们使用更为安全的https协议可以看的出来,苹果公司是对安全很重视的一家公司,为什么在这里我会提到安全呢?是因为之前我们的推送内容,不管是通过第三方,还是通过苹果自带的通知处理,如果让有心人对数据做一次拦截,抓个包啥的,我们推送的内容就会完全暴露,当然有的同学说,我可以加密啊~但是不知道大家有没有想过,如果数据加密,那通知栏会怎么展示呢?(你千万别跟我说你把所有的远程推送变成本地通知。。)通过此次这次增加的UNNotificationServiceExtension的类,便可以更好的帮助我们实现数据的加密。
    它的原理便是在收到通知后的最多30s内,你可以把你的通知内容,解密后,在重新展示在用户的通知拦上。
    2、 内容的丰富
    之前的通知展示内容比较少,以至于被各种广告提醒占据了。这次苹果新添加的附件通知,结合上通知拓展类,便可以给用户展现出一个有着丰富内容的通知。比如,一个小短片的某一秒的画面啊(这里要强烈鄙视各大平台的某些电影预览图),又或者是配上一些小图片啊(通过服务器传来的imaUrl)等方式来吸引用户,诱导用户点开你的通知,促使用户会使用你的App。其实推送这个功能,虽然有的人会关闭,但是大部分的人还是开启的,所以说推送这个市场还是很大的哟~灵活利用推送,会让你的程序拥有无限的可能。

如何新建一个UNNotificationServiceExtension

  • 这里不需要自己创建类来使用

打开工程,按照图示位置打开Target

  • File->New->Target…
  • 选择NotificationServiceExtension,然后命名target名字
  • 执行完以上操作,然后会生成一个文件夹,如图所示:
  • 与此同时,TARGETS内会生成一个target,如图所示:
  • 从上图中我们可以看到其bundleID是由两部分组成,工程的bundleID+命名的target名字,DisplayName是命名的target名字。

如何使用创建的文件

  • 处理通知的内容
- (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];
    // 我们可以在此重写一些东西
    // 重写一些东西
    self.bestAttemptContent.title = @"我是标题";
    self.bestAttemptContent.subtitle = @"我是副标题";
    self.bestAttemptContent.body = @"信息内容";

    // 附件
    NSDictionary *dict =  self.bestAttemptContent.userInfo;
    NSDictionary *notiDict = dict[@"aps"];
    NSString *imgUrl = [NSString stringWithFormat:@"%@",notiDict[@"imageAbsoluteString"]];
    // 如果不需要下载,直接展示通知
    if (!imgUrl.length) {
        self.contentHandler(self.bestAttemptContent);
    }
    [self loadAttachmentForUrlString:imgUrl withType:@"png" completionHandle:^(UNNotificationAttachment *attach) {

        if (attach) {
            self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];
        }
        self.contentHandler(self.bestAttemptContent);

    }];
}
  • 自定义,利用系统自带类进行附件的下载
  • 创建附件的url,必须是一个文件路径,也就是说,必须下载下,才能获取文件路径,开头是file://
- (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];

                        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];
}

需要注意的

附件内容的大小是由限制的

ios 远程推送p12证书_推送_02

如果调试这个通知扩展类

  • 选择正确的target,如图所示:
  • 选择原本的应用程序

推送内容格式如下

  • 一定要有"mutable-content": "1",以及一定要有Alert的字段,否则可能会拦截通知失败。
{
    "aps": {
        "alert": "This is some fancy message.",
        "badge": 1,
        "sound": "default",
        "mutable-content": "1",
        "imageAbsoluteString": "http://upload.univs.cn/2012/0104/1325645511371.jpg"

    }
}

代码地址:
https://github.com/FlyingKuiKui/PushTestApp.git