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值需要跟后臺開發(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) {
// 臨時(shí)文件夾路徑,APP沒有運(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)用在極光平臺的AppKey"];
[JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
NSLog(@"apns upload success");
self.contentHandler(self.bestAttemptContent);
}];
}
@end
注意事項(xiàng)
如果傳了圖片地址卻還不顯示,不要驚慌,先請確保圖片別太大,而且可以使用NSURLSession下載,否則就會(huì)出現(xiàn)圖片不顯示的問題。
以上就是iOS推送增加右側(cè)顯示圖Service Extension的詳細(xì)內(nèi)容,更多關(guān)于iOS 推送增加右側(cè)顯示圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解iOS應(yīng)用開發(fā)中的ARC內(nèi)存管理方式
這篇文章主要介紹了詳解iOS應(yīng)用開發(fā)中的ARC內(nèi)存管理方式,文中示例基于Objective-C語言,需要的朋友可以參考下2016-02-02
IOS 時(shí)間和時(shí)間戳之間轉(zhuǎn)化示例
我們經(jīng)常從服務(wù)器后臺拿到時(shí)間戳的時(shí)間,以下代碼可以實(shí)現(xiàn)將時(shí)間戳轉(zhuǎn)為可讀的時(shí)間格式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
iOS中監(jiān)聽UITextField值改變事件的方法實(shí)例
UITextField 是一個(gè)用來處理文本輸入和現(xiàn)實(shí)的控件,在我們的開發(fā)當(dāng)中也是經(jīng)常被用到。下面這篇文章主要給大家介紹了關(guān)于iOS中監(jiān)聽UITextField值改變事件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法
本篇文章主要介紹了iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

