安卓小伙伴跟我講你們的push可以放圖片了,還不趕緊做,我們安卓可以放。好吧既然你有這個愿望我就滿足你!
實現(xiàn)
這里只完成了一個簡單的在push邊上多加一張圖,因為沒有要加其他拓展(其實我是懶)
不多說直接貼代碼。
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSURL *url = [NSURL URLWithString:request.content.userInfo[@"image"]];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"download attachment image error : %@", error);
}else{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject
stringByAppendingPathComponent:@"download"];
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:path]) {
[manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *fileName = [NSString stringWithFormat:@"%lld.jpg", (long long)[[NSDate date] timeIntervalSince1970] * 1000];
path = [path stringByAppendingPathComponent:fileName];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"path : %@", path);
NSError *err = nil;
[UIImageJPEGRepresentation(image, 1) writeToFile:path options:NSAtomicWrite error:&err];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"remote-atta1" URL:[NSURL fileURLWithPath:path] options:nil error:&err];
if (attachment) {
self.bestAttemptContent.attachments = @[attachment];
}
}
self.contentHandler(self.bestAttemptContent);
}];
[task resume];
}
先通過左上角的file -> New ->target增加一個NotificationService的extension就好了。然后里面提供了2個方法。將如上代碼放入第一個方法中就完成了。這里要注意的點是:
第一點
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
notificationCenter.delegate = [UIApplication sharedApplication].delegate;
[notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"%d--%@", granted, error);
}];;
[notificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
注冊通知的時候記得區(qū)分版本。
第二點(真的好氣啊)
我在測試是時候用的是smartpush。一個push的測試小工具,我通過request.content.userInfo將image取出來(這里完全是根據(jù)你自己push過來的數(shù)據(jù)格式?jīng)Q定)。這時候我發(fā)現(xiàn)通知一直有而圖就是沒有出來。為什么會這樣。是我哪里實現(xiàn)有問題嗎?測試了大概一天,我突然想難道圖有毒,我就換了一張圖。意外的發(fā)現(xiàn)原來圖的尺寸是有問題的,之前用的是一張大圖雖然大小只有97KB但是依舊無法加載。之后換了一張小尺寸的成功了!當(dāng)時就喜極而泣。如果有小伙伴要測試。一定要用一張小尺寸的圖。不然你可能跟我一樣悲慘。