1. 程式人生 > >通知實戰 設置通知圖片(iOS10以後的)

通知實戰 設置通知圖片(iOS10以後的)

擴展 分享圖片 toc version src 設置 ESS ast 沒有

解釋兩個基本擴展(Notification ContentNotification Service

Notification Content其實是用來自定義長按通知顯示通知的自定義界面

Notification Service是用來處理遠程通知的,我們可以在遠程通知到來之際,我們在Notification Service

裏面由30s的時間來處理這條通知的

首先使用Notification Service

1.創建擴展target

技術分享圖片

2. 這裏會新建一個擴展的target,新的target需要新的ID,新的證書等(證書和id不會的請百度)

技術分享圖片

3.然後對主target進行設置

技術分享圖片

4. 對擴展的target進行設置

技術分享圖片

5.這裏用的是極光推送,需要極光的相關文件(需要文件的可以去極光開發中心獲取demo)

技術分享圖片

6.擴展target添加文件

技術分享圖片

7.當添加後為了可以訪問http,在對應的plist文件進行設置

技術分享圖片

8.設置完成之後就可以看代碼了

技術分享圖片

#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];
// self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [NotificationService]", self.bestAttemptContent.title]; NSString * attachmentPath = self.bestAttemptContent.userInfo[@"myAttachmentURL"]; //if exist if (attachmentPath) { //download NSURL *fileURL = [NSURL URLWithString:attachmentPath]; [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)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler { NSURLSession * session = [NSURLSession sharedSession]; NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSString *localPath = nil; if (!error) { 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 { //please invoke this func on release version //[JPushNotificationExtensionService setLogOff]; //service extension sdk //upload to calculate delivery rate [JPushNotificationExtensionService jpushSetAppkey:@"3a6190XXXXXXX28907"]; [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ { NSLog(@"apns upload success"); self.contentHandler(self.bestAttemptContent); }]; } - (void)serviceExtensionTimeWillExpire { self.contentHandler(self.bestAttemptContent); } @end

其中的 "myAttachmentURL" 是自己定義的即可 (代碼請仔細閱讀,原理是獲取網絡圖片,然後再顯示)

9. 通過極光進行推送測試(目標平臺選擇開發環境即可,證書配置,請參考極光文檔)

技術分享圖片

10.設置參數, 測試開始完成推送. "myAttachmentURL"是自己設置的哦!記得設置 mutable-content

技術分享圖片

11.收到推送

技術分享圖片

12.Notification Service斷點調試說明

技術分享圖片

選擇target時並沒有我們的擴展target,需要點擊管理target進而點擊Autocreate Schemes Now

技術分享圖片

技術分享圖片

這個時候擴展的target出現了

技術分享圖片

好了可以運行了(會進行一次選擇,選擇我們的主app即可,運行後在 NotificationService.m 文件中的斷點就可以捕捉到啦)

技術分享圖片

到這裏Notification Service的使用說明就結束了,以後有補充的再寫啦!

通知實戰 設置通知圖片(iOS10以後的)