android接收富文本推送,iOS10富文本推送--NotificationServiceExtension

NotificationService文件

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

@interface NotificationService ()

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

@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@property (nonatomic, strong) NSFileManager *fileMgr;

@end

@implementation NotificationService

LazyLoad

-(NSFileManager *)fileMgr{

return [NSFileManager defaultManager];

}

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

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

self.contentHandler = contentHandler;

self.bestAttemptContent = [request.content mutableCopy];

NSDictionary * userInfo = request.content.userInfo;

NSString * attchUrl = userInfo[@"aps"][@"image"];

NSString *exetension = [NSString stringWithFormat:@".%@",[[attchUrl componentsSeparatedByString:@"."] lastObject]];

if (attchUrl) {

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

NSURL *url = [NSURL URLWithString:attchUrl];

NSURLSessionDownloadTask *download = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable tempLocation, NSURLResponse * _Nullable response, NSError * _Nullable error) {

if (!error) {

NSURL *localURL = [NSURL fileURLWithPath:[tempLocation.path stringByAppendingString:exetension]];

[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);

}else if (attachment){

self.bestAttemptContent.attachments = @[attachment];

}

}else{

NSLog(@"%@",error.localizedDescription);

}

self.bestAttemptContent.categoryIdentifier = userInfo[@"aps"][@"category"];//这里设置这个类别的标识符

self.contentHandler(self.bestAttemptContent);//回调给系统

}];

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值