iOS推送增加右側(cè)顯示圖Service Extension
正文
本Demo推送使用的是極光推送(換成其他推送改動(dòng)也不大)極光文檔 極光Demo
先看下效果圖,在系統(tǒng)的推送彈窗右側(cè)增加了一個(gè)圖片
工程配置(一)
- 首先需要一個(gè)已經(jīng)集成了極光推送,并且可以正常接收推送的工程(參考極光文檔again);
- 配置主Target,如下截圖所示,勾選主Target的Background Modes;
- 創(chuàng)建Service Extension,看下面的三圖;
- 給創(chuàng)建好的PushExtension(子Target)配置Push Notifications,這一步操作就和主Target的配置推送一樣;
工程配置(二)集成JPushExtension
這一步是按照需求可選的,引入JPushExtension的目的是為了極光推送做統(tǒng)計(jì)
處理推送顯示的內(nèi)容
這是配置好的工程目錄,多了一個(gè)PushExtention
文件夾
NotificationService.m
文件的內(nèi)容改為如下
#import "NotificationService.h" #import "JPushNotificationExtensionService.h" @interface NotificationService () @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; @end @implementation NotificationService - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // 讀取圖片地址,并加載 NSString *imgUrl = [NSString stringWithFormat:@"%@", self.bestAttemptContent.userInfo[@"imageUrl"]]; // ??圖片字段的key值需要跟后臺(tái)開(kāi)發(fā)統(tǒng)一 if (imgUrl) { NSURL *fileURL = [NSURL URLWithString:imgUrl]; [self downloadAndSave:fileURL handler:^(NSString *localPath) { if (localPath) { UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"myAttachment" URL:[NSURL fileURLWithPath:localPath] options:nil error:nil]; self.bestAttemptContent.attachments = @[attachment]; } [self apnsDeliverWith:request]; }]; } else { [self apnsDeliverWith:request]; } } - (void)serviceExtensionTimeWillExpire { self.contentHandler(self.bestAttemptContent); } #pragma mark - 私有方法 - (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler { // 這里需要用系統(tǒng)網(wǎng)絡(luò)請(qǐng)求來(lái)下載圖片 NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSString *localPath = nil; if (!error) { // 臨時(shí)文件夾路徑,APP沒(méi)有運(yùn)行時(shí)會(huì)自動(dòng)清除圖片,不會(huì)占用內(nèi)存 NSString *localURL = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), fileURL.lastPathComponent]; if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:localURL error:nil]) { localPath = localURL; } } handler(localPath); }]; [task resume]; } - (void)apnsDeliverWith:(UNNotificationRequest *)request { [JPushNotificationExtensionService jpushSetAppkey:@"本應(yīng)用在極光平臺(tái)的AppKey"]; [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ { NSLog(@"apns upload success"); self.contentHandler(self.bestAttemptContent); }]; } @end
注意事項(xiàng)
如果傳了圖片地址卻還不顯示,不要驚慌,先請(qǐng)確保圖片別太大,而且可以使用NSURLSession下載,否則就會(huì)出現(xiàn)圖片不顯示的問(wèn)題。
以上就是iOS推送增加右側(cè)顯示圖Service Extension的詳細(xì)內(nèi)容,更多關(guān)于iOS 推送增加右側(cè)顯示圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS開(kāi)發(fā)避免安全隱患的要點(diǎn)總結(jié)
在本篇文章里小編給各位整理了關(guān)于iOS開(kāi)發(fā)如何避免安全隱患的知識(shí)點(diǎn)總結(jié),需要的朋友們學(xué)習(xí)下。2019-07-07iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對(duì)齊
這篇文章主要介紹了iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對(duì)齊的相關(guān)資料,需要的朋友可以參考下2015-09-09iOS 實(shí)現(xiàn)類似QQ分組樣式的兩種方式
這篇文章主要介紹了iOS 實(shí)現(xiàn)類似QQ分組樣式的兩種方式,思路很簡(jiǎn)單,對(duì)模型數(shù)據(jù)操作或則控制界面顯示,需要的朋友可以參考下2017-07-07iOS實(shí)現(xiàn)知乎和途家導(dǎo)航欄漸變的文字動(dòng)畫效果
這篇文章給大家分享了利用iOS實(shí)現(xiàn)知乎和途家導(dǎo)航欄漸變的文字動(dòng)畫效果,有需要的朋友們可以參考借鑒。下面來(lái)一起看看。2016-09-09詳解iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)于享元模式的運(yùn)用
這篇文章主要介紹了iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)于享元模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04iOS UIWebView 通過(guò) cookie 完成自動(dòng)登錄實(shí)例
本篇文章主要介紹了iOS UIWebView 通過(guò) cookie 完成自動(dòng)登錄實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04Android中g(shù)etActivity()為null的解決辦法
在Android開(kāi)發(fā)的時(shí)候可能遇過(guò)出現(xiàn)getActivity()出現(xiàn)null的時(shí)候?qū)е鲁绦驁?bào)出空指針異常,那么遇到這種情況改如何解決,下面跟著小編一起去看看。2016-08-08