通知实战 设置通知图片(iOS10以后的)
解釋兩個基本擴展(Notification Content、Notification Service)
Notification Content其實是用來自定義長按通知顯示通知的自定義界面
Notification Service是用來處理遠程通知的,我們可以在遠程通知到來之際,我們在Notification Service
里面由30s的時間來處理這條通知的
首先使用Notification Service
1.創建擴展target
2. 這里會新建一個擴展的target,新的target需要新的ID,新的證書等(證書和id不會的請百度)
3.然后對主target進行設置
注意如果沒有用到后臺的播放什么的只選Remote notifications,免得被拒絕
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以后的)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 电脑屏保海底世界_水下栖息地:人类能否在
- 下一篇: 比特币Merkle树和SPV机制
