通知实战 设置通知图片(iOS10以后的)
解釋兩個(gè)基本擴(kuò)展(Notification Content、Notification Service)
Notification Content其實(shí)是用來自定義長按通知顯示通知的自定義界面
Notification Service是用來處理遠(yuǎn)程通知的,我們可以在遠(yuǎn)程通知到來之際,我們在Notification Service
里面由30s的時(shí)間來處理這條通知的
首先使用Notification Service
1.創(chuàng)建擴(kuò)展target
2. 這里會(huì)新建一個(gè)擴(kuò)展的target,新的target需要新的ID,新的證書等(證書和id不會(huì)的請百度)
3.然后對主target進(jìn)行設(shè)置
注意如果沒有用到后臺的播放什么的只選Remote notifications,免得被拒絕
4. 對擴(kuò)展的target進(jìn)行設(shè)置
5.這里用的是極光推送,需要極光的相關(guān)文件(需要文件的可以去極光開發(fā)中心獲取demo)
6.擴(kuò)展target添加文件
7.當(dāng)添加后為了可以訪問http,在對應(yīng)的plist文件進(jìn)行設(shè)置
8.設(shè)置完成之后就可以看代碼了
#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" 是自己定義的即可 (代碼請仔細(xì)閱讀,原理是獲取網(wǎng)絡(luò)圖片,然后再顯示)
9. 通過極光進(jìn)行推送測試(目標(biāo)平臺選擇開發(fā)環(huán)境即可,證書配置,請參考極光文檔)
10.設(shè)置參數(shù), 測試開始完成推送."myAttachmentURL"是自己設(shè)置的哦!記得設(shè)置mutable-content
11.收到推送
12.Notification Service斷點(diǎn)調(diào)試說明
選擇target時(shí)并沒有我們的擴(kuò)展target,需要點(diǎn)擊管理target進(jìn)而點(diǎn)擊Autocreate Schemes Now
這個(gè)時(shí)候擴(kuò)展的target出現(xiàn)了
好了可以運(yùn)行了(會(huì)進(jìn)行一次選擇,選擇我們的主app即可,運(yùn)行后在NotificationService.m 文件中的斷點(diǎn)就可以捕捉到啦)
到這里Notification Service的使用說明就結(jié)束了,以后有補(bǔ)充的再寫啦!
總結(jié)
以上是生活随笔為你收集整理的通知实战 设置通知图片(iOS10以后的)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑屏保海底世界_水下栖息地:人类能否在
- 下一篇: CStatic的透明背景方法