iOS推送增加右側顯示圖Service Extension
更新時間:2022年10月11日 16:48:46 作者:ZouNiMei
這篇文章主要為大家介紹了iOS推送增加右側顯示圖Service Extension,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
正文
本Demo推送使用的是極光推送(換成其他推送改動也不大)極光文檔 極光Demo
先看下效果圖,在系統(tǒng)的推送彈窗右側增加了一個圖片

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

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



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

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

處理推送顯示的內容
這是配置好的工程目錄,多了一個PushExtention文件夾

NotificationService.m文件的內容改為如下
#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)絡請求來下載圖片
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沒有運行時會自動清除圖片,不會占用內存
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:@"本應用在極光平臺的AppKey"];
[JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
NSLog(@"apns upload success");
self.contentHandler(self.bestAttemptContent);
}];
}
@end
注意事項
如果傳了圖片地址卻還不顯示,不要驚慌,先請確保圖片別太大,而且可以使用NSURLSession下載,否則就會出現(xiàn)圖片不顯示的問題。
以上就是iOS推送增加右側顯示圖Service Extension的詳細內容,更多關于iOS 推送增加右側顯示圖的資料請關注腳本之家其它相關文章!
相關文章
iOS中監(jiān)聽UITextField值改變事件的方法實例
UITextField 是一個用來處理文本輸入和現(xiàn)實的控件,在我們的開發(fā)當中也是經(jīng)常被用到。下面這篇文章主要給大家介紹了關于iOS中監(jiān)聽UITextField值改變事件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07

