iOS推送增加右側(cè)顯示圖Service Extension
正文
本Demo推送使用的是極光推送(換成其他推送改動也不大)極光文檔 極光Demo
先看下效果圖,在系統(tǒng)的推送彈窗右側(cè)增加了一個圖片

工程配置(一)
- 首先需要一個已經(jīng)集成了極光推送,并且可以正常接收推送的工程(參考極光文檔again);
- 配置主Target,如下截圖所示,勾選主Target的Background Modes;

- 創(chuàng)建Service Extension,看下面的三圖;



- 給創(chuàng)建好的PushExtension(子Target)配置Push Notifications,這一步操作就和主Target的配置推送一樣;

工程配置(二)集成JPushExtension
這一步是按照需求可選的,引入JPushExtension的目的是為了極光推送做統(tǒng)計

處理推送顯示的內(nèi)容
這是配置好的工程目錄,多了一個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值需要跟后臺開發(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ò)請求來下載圖片
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *localPath = nil;
if (!error) {
// 臨時文件夾路徑,APP沒有運行時會自動清除圖片,不會占用內(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)用在極光平臺的AppKey"];
[JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
NSLog(@"apns upload success");
self.contentHandler(self.bestAttemptContent);
}];
}
@end
注意事項
如果傳了圖片地址卻還不顯示,不要驚慌,先請確保圖片別太大,而且可以使用NSURLSession下載,否則就會出現(xiàn)圖片不顯示的問題。
以上就是iOS推送增加右側(cè)顯示圖Service Extension的詳細內(nèi)容,更多關(guān)于iOS 推送增加右側(cè)顯示圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對齊
這篇文章主要介紹了iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對齊的相關(guān)資料,需要的朋友可以參考下2015-09-09
iOS實現(xiàn)知乎和途家導(dǎo)航欄漸變的文字動畫效果
這篇文章給大家分享了利用iOS實現(xiàn)知乎和途家導(dǎo)航欄漸變的文字動畫效果,有需要的朋友們可以參考借鑒。下面來一起看看。2016-09-09
詳解iOS App設(shè)計模式開發(fā)中對于享元模式的運用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對于享元模式的運用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
iOS UIWebView 通過 cookie 完成自動登錄實例
本篇文章主要介紹了iOS UIWebView 通過 cookie 完成自動登錄實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Android中g(shù)etActivity()為null的解決辦法
在Android開發(fā)的時候可能遇過出現(xiàn)getActivity()出現(xiàn)null的時候?qū)е鲁绦驁蟪隹罩羔槷惓?,那么遇到這種情況改如何解決,下面跟著小編一起去看看。2016-08-08

