創(chuàng)建Notification Service Extension
創(chuàng)建的Service Extension會(huì)自動(dòng)創(chuàng)建NotificationService文件
具體實(shí)現(xiàn)的Demo就不放了韩容,代碼也比較簡單,就是提供一些思路而已膏蚓,有疑問可以留言案狠,盡量回復(fù)
收到通知之后的處理
- 首先我們需要確定我們的通知可展示的素材類型牧挣,我們暫時(shí)支持png,gif,mp4三種類型的素材
#define fileNameArray @[@"notificationImage.png",@"notificationGif.gif",@"notificationVideo.mp4"]//定義的素材下載之后存儲(chǔ)的文件名稱
#define identifierArray @[@"ImageNotification",@"GifNotification",@"VideoNotification"]//定義通知素材的Identifier
- 定義enum表示通知展示的類型
typedef enum : NSUInteger {
NotificationImageType,
NotificationGifType,
NotificationVideoType
} NotificationType;
- 通知中自定義增加的素材參數(shù)
1晒他、imageUrl表示圖片素材
2赶诊、videoUrl表示視頻素材
3笼平、gifUrl表示gif圖素材
4、subTitle表示通知的子標(biāo)題
這些字段均在個(gè)推通知的payload中存在甫何,根據(jù)不同的字段展示不同的素材
- 通知具體的處理
這里我們以個(gè)推為例出吹,個(gè)推通知包含payload字段,我們可以在payload字段中定義通知展示的類型,通知的主要處理在方法:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// Modify the notification content here...
//個(gè)推userinfo中的payload解析出來是NSString類型辙喂,需要轉(zhuǎn)換成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) {
//視頻的展示最好判斷當(dāng)前的網(wǎng)絡(luò)狀態(tài)是否為Wifi捶牢,防止用戶流量的流失,引入Reachability文件巍耗,可判斷當(dāng)前網(wǎng)絡(luò)是否為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.存儲(chǔ)通知附帶的素材文件
Tips:如果應(yīng)用不支持ATS,請(qǐng)?jiān)贓xtension的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];
}