富文本推送

iOS10富文本推送--NotificationServiceE

2017-07-18  本文已影响538人  叫我龙哥

title: iOS10富文本推送--NotificationServiceExtension
date: 2017-07-18 14:47:04
tags: 原创分享


添加http协议支持,没错,这里是支持http协议的,不像其他文章说的不支持
配置在另外基础篇文章里面有,info.plist文件里修改一下就行了

NotificationService info.plist

NotificationService文件

额外添加了一个文件管理器的字段,用来存储数据

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@property (nonatomic, strong) NSFileManager *fileMgr;
@property (nonatomic, strong) NSURLSessionDownloadTask *download;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSDictionary *userInfo;
@property (nonatomic, strong) NSURL *attchUrl;
@property (nonatomic, strong) NSString *imageExtension;
@end

@implementation NotificationService

LazyLoad

-(NSFileManager *)fileMgr{
    return [NSFileManager defaultManager];
}

网络session

-(NSURLSession *)session{
    if (_session == nil) {
        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    }
    return _session;
}

下载任务

-(NSURLSessionDownloadTask *)download{
    if (!_download) {
       _download = [self.session downloadTaskWithURL:self.attchUrl completionHandler:^(NSURL * _Nullable tempLocation, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (!error) {
                NSURL *localURL = [NSURL fileURLWithPath:[tempLocation.path stringByAppendingString:self.imageExtension]];
                [self.fileMgr moveItemAtURL:tempLocation toURL:localURL error:&error];
                NSError *attachmentError = nil;
                UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"photo" URL:localURL options:nil error:&attachmentError];
                if (attachmentError) {
                    NSLog(@"attachmentError %@",attachmentError);
                }else if (attachment){
                    self.bestAttemptContent.attachments = @[attachment];
                }else{
                    
                }
            }else{
                NSLog(@"downloadTaskerror %@",error.localizedDescription);
            }
            self.bestAttemptContent.categoryIdentifier = self.userInfo[@"aps"][@"category"];
            self.contentHandler(self.bestAttemptContent);
        }];
    }
    return _download;
}

收到远程通知之后,在当前方法内进行处理,并生成attchment,最终回调给系统

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
   self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    self.resumeTime = 0;
    self.userInfo = [request.content.userInfo copy];
    NSString * attchUrl = self.userInfo[@"image"];
    self.imageExtension = [NSString stringWithFormat:@".%@",[[attchUrl componentsSeparatedByString:@"."] lastObject]];
    if (attchUrl) {
        self.attchUrl = [NSURL URLWithString:attchUrl];
        [self resumeSession];
    }
}

开始执行下载多媒体资源任务

- (void)resumeSession{
    [self.download resume];
}

超时,异常时调用

- (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
上一篇下一篇

猜你喜欢

热点阅读