簡(jiǎn)介
UserNotifications.framework框架是iOS 10中一個(gè)重要的更新碰辅,它統(tǒng)一了舊版本雜亂的和通知相關(guān)的API.
iOS 10后最大的特色是可以支持多媒體推送爹谭,包括圖像jpg杯巨、gif以及聲音及視頻等,另外還可以自定義推送視圖练般、撤回通知以及在不增加推送信息數(shù)量的情況下更新推送內(nèi)容等等矗漾。
快速集成步驟
1、新的請(qǐng)求推送權(quán)限
//彈出允許權(quán)限通知提示不應(yīng)該在app啟動(dòng)的時(shí)候薄料,應(yīng)該在程序需要的地方調(diào)用敞贡,僅提示一次
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate=self;
UNAuthorizationOptions types10=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//點(diǎn)擊允許
NSLog(@"允許");
} else {
//點(diǎn)擊不允許
NSLog(@"不允許");
}
}];
2、新的接收及處理推送信息
//iOS10新增:處理前臺(tái)收到通知的代理方法
(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//應(yīng)用處于前臺(tái)時(shí)的遠(yuǎn)程推送接受
//關(guān)閉友盟自帶的彈出框
[UMessage setAutoAlert:NO];
//必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//應(yīng)用處于前臺(tái)時(shí)的本地推送接受
}
//當(dāng)應(yīng)用處于前臺(tái)時(shí)提示設(shè)置摄职,需要哪個(gè)可以設(shè)置哪一個(gè)
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
//iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
[[YKPushNotificationManager shareInstance]dealWithNotificationBackgroundIniOS10:response Completion:completionHandler];
}
多媒體推送
1誊役、工程中導(dǎo)入
UserNotifications.framework以及UserNotificationsUI.framework
2获列、在開(kāi)發(fā)者賬戶中新建對(duì)應(yīng)項(xiàng)目bundle ID對(duì)應(yīng)的WildCardAppId:
如工程的bundle ID為:com.test.xxx
則wildCardAppID為 :com.test.xxx.*
3、如果需要在推送信息中顯示圖像蛔垢、聲音或者視頻击孩,需要建立NotificationService與NotificationContent兩個(gè)target,其中NotificationServiceExtendsion用來(lái)處理推送中的多媒體資源鹏漆,
NotificationContentExtentsion用來(lái)處理自定義視圖巩梢。
新建的兩個(gè)target都包含了info.plist文件,如果需要在推送中顯示多媒體資源艺玲,多媒體中的鏈接要求為https括蝠,否則需要在需要在info.plist中把ATS Setting設(shè)置為yes。
ContentTarget中的info.plist內(nèi)的UNNotificationExtensionCategory表示推送的類(lèi)別標(biāo)識(shí)饭聚,推送信息中帶上這個(gè)標(biāo)識(shí)可以顯示自定義視圖以及自定義的按鈕交互忌警。
NotificationServiceExtendsion中生成了兩個(gè)自帶的方法,
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)requestwithContentHandler:用于處理接受推送后用來(lái)請(qǐng)求媒體資源秒梳,
另一個(gè)用于請(qǐng)求超時(shí)的處理操作
- (void)serviceExtensionTimeWillExpire
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
NSDictionary *apsDic = [request.content.userInfo objectForKey:@"aps"];
//設(shè)定標(biāo)識(shí)交互
NSString *category = [apsDic objectForKey:@"category"];
self.bestAttemptContent.categoryIdentifier = category;
NSString *imageUrl = [apsDic objectForKey:@"image"];
NSString *gifUrl = [apsDic objectForKey:@"gif"];
NSString *videoUrl = [apsDic objectForKey:@"video"];
NSString *audioUrl = [apsDic objectForKey:@"audio"];
NSString *typeString;
NSURL *url;
if (imageUrl.length > 0)
{
url = [NSURL URLWithString:imageUrl];
typeString = @"jpg";
}
if (gifUrl.length > 0)
{
url = [NSURL URLWithString:gifUrl];
typeString = @"gif";
}
if (videoUrl.length > 0)
{
url = [NSURL URLWithString:videoUrl];
typeString = @"mp4";
}
if (audioUrl.length > 0)
{
url = [NSURL URLWithString:audioUrl];
typeString = @"mp3";
}
NSURLSession *session = [NSURLSession sharedSession];
if (url)
{
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url completionHandler:
^(NSURL * _Nullable location,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
{
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *toPath = [caches stringByAppendingString:[NSString stringWithFormat:@".%@",typeString]];
NSLog(@"fileToPath = %@",toPath);
// 將臨時(shí)文件剪切或者復(fù)制Caches文件夾
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路徑
// ToPath : 剪切后的文件路徑
[mgr moveItemAtPath:location.path toPath:toPath error:nil];
if (toPath && ![toPath isEqualToString: @""])
{
UNNotificationAttachment *attch = [UNNotificationAttachment attachmentWithIdentifier:@""
URL:[NSURL URLWithString:[@"file://" stringByAppendingString:toPath]]
options:nil
error:nil];
if(attch)
{
self.bestAttemptContent.attachments = @[attch];
}
}
self.contentHandler(self.bestAttemptContent);
}];
[downloadTask resume];
}
else
{
self.contentHandler(self.bestAttemptContent);
}
}
- (void)serviceExtensionTimeWillExpire {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
self.contentHandler(self.bestAttemptContent);
}
本地推送
//能夠通過(guò)identitifier來(lái)區(qū)別其他通知法绵,并且能夠進(jìn)行本地消息的更新、尋找酪碘、刪除等朋譬,其中更新通知能夠在不增加推送條數(shù)的情況下更新內(nèi)容。
NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
content:content
trigger:trigger1];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
category id 必須對(duì)應(yīng) Notification info.plist 里面才能顯示自定義交互
每一個(gè)推送內(nèi)容最好帶上一個(gè)catagory婆跑,這樣方便我們根據(jù)不同的categoryId來(lái)自定義不同類(lèi)型的視圖,以及自定義交互庭呜。
有可能遇到的坑:
一滑进、不能收到多媒體信息,仍然顯示普通文本
解決方法:
1募谎、推送信息中檢查是否包含: "mutable-content" = 1 扶关,通知權(quán)限要設(shè)置UNNotificationPresentationOptionAlert
2、更換不同的系統(tǒng)版本数冬,在測(cè)試中發(fā)現(xiàn)10.3系統(tǒng)終端能收到节槐,10.2.1系統(tǒng)不能收到
二、調(diào)試的時(shí)候要選擇對(duì)應(yīng)的target才能斷點(diǎn)