iOS--富文本推送NotificationServiceExtension


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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卟败灬筱龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值