创建Notification Service Extension
创建的Service Extension会自动创建NotificationService文件
具体实现的Demo就不放了,代码也比较简单,就是提供一些思路而已,有疑问可以留言,尽量回复
收到通知之后的处理
- 首先我们需要确定我们的通知可展示的素材类型,我们暂时支持png,gif,mp4三种类型的素材
#define fileNameArray @[@"notificationImage.png",@"notificationGif.gif",@"notificationVideo.mp4"]//定义的素材下载之后存储的文件名称
#define identifierArray @[@"ImageNotification",@"GifNotification",@"VideoNotification"]//定义通知素材的Identifier
- 定义enum表示通知展示的类型
typedef enum : NSUInteger {
NotificationImageType,
NotificationGifType,
NotificationVideoType
} NotificationType;
- 通知中自定义增加的素材参数
1、imageUrl表示图片素材
2、videoUrl表示视频素材
3、gifUrl表示gif图素材
4、subTitle表示通知的子标题
这些字段均在个推通知的payload中存在,根据不同的字段展示不同的素材
- 通知具体的处理
这里我们以个推为例,个推通知包含payload字段,我们可以在payload字段中定义通知展示的类型,通知的主要处理在方法:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// Modify the notification content here...
//个推userinfo中的payload解析出来是NSString类型,需要转换成NSDictionary
NSString *payloadstr = [request.content.userInfo objectForKey:@"payload"];
NSData *jsondata = [payloadstr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *payload = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingMutableContainers error:nil];
if (payload) {
NSString *subTitle = [payload objectForKey:@"subTitle"];
self.bestAttemptContent.subtitle = subTitle ? subTitle : @"";
NSString *imageUrl = [payload objectForKey:@"imageUrl"];
NSString *videoUrl = [payload objectForKey:@"videoUrl"];
NSString *gifUrl = [payload objectForKey:@"gifUrl"];
if (imageUrl && imageUrl.length !=0) {
[self savePath:imageUrl dataType:NotificationImageType];
} else if (videoUrl && videoUrl.length != 0) {
//视频的展示最好判断当前的网络状态是否为Wifi,防止用户流量的流失,引入Reachability文件,可判断当前网络是否为Wifi
if (Reachability) {//这里判断的条件我们采用的自己封装的类,就不贴代码了
[self savePath:videoUrl dataType:NotificationVideoType];
} else {
self.contentHandler(self.bestAttemptContent);
}
} else if (gifUrl && gifUrl.length != 0) {
[self savePath:gifUrl dataType:NotificationGifType];
}else {
self.contentHandler(self.bestAttemptContent);
}
}
}
5.存储通知附带的素材文件
Tips:如果应用不支持ATS,请在Extension的plist增加ATS的配置,否则素材下载失败
- (void)savePath:(NSString*)path dataType:(NotificationType)type {
NSString *templatePath = NSTemporaryDirectory();
NSString *filePath = @"";
if (type < fileNameArray.count) {
filePath = [NSString stringWithFormat:@"%@/%@",templatePath,fileNameArray[type]];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
[fileManager removeItemAtPath:filePath error:nil];
}
[[[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:path] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (location) {
NSError *moveError = nil;
[[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:&moveError];
if (!error) {
UNNotificationAttachment *attachment;
if (type < identifierArray.count) {
attachment = [UNNotificationAttachment attachmentWithIdentifier:identifierArray[type] URL:[NSURL fileURLWithPath:filePath] options:nil error:nil];
}
if (attachment) {
self.bestAttemptContent.attachments = @[attachment];
}
}
}
self.contentHandler(self.bestAttemptContent);
}] resume];
}