iOS10的通知增加圖片的實現(xiàn)(一個坑)

安卓小伙伴跟我講你們的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)時就喜極而泣。如果有小伙伴要測試。一定要用一張小尺寸的圖。不然你可能跟我一樣悲慘。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,872評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,143評論 4 61
  • 上一章:乘龍引目錄 旦夕之間,江湖百載。光陰流沙,時不我待。 憶當(dāng)年意氣甚高,怎忍道蕭騷今朝...
    劍仙裴宣閱讀 573評論 0 10
  • 白墻,綠床單,古銅金屬吊燈,孫游坐在桌前,窗外陽光耀眼,她從左抽屜里取出一個鐵盒,小心地將里面的照片攤在桌子上,她...
    喬未晚閱讀 468評論 0 0

友情鏈接更多精彩內(nèi)容