比來在研討IOS10關於推送的新特征, 比擬之前確切做了很年夜的轉變,總結起來重要是以下幾點:
1.推送內容加倍豐碩,由之前的alert 到如今的title, subtitle, body
2.推送同一由trigger觸發
3.可認為推送增長附件,如圖片、音頻、視頻,這就使推送內容加倍豐碩多彩
4.可以便利的更新推送內容
import 新框架
添加新的框架 UserNotifications.framework
#import <UserNotifications/UserNotifications.h>
注冊推送
在設置告訴的時刻,須要先輩行注冊,獲得受權
IOS10 一切告訴都是經由過程UNUserNotificationCenter來治理,包含長途告訴和當地告訴
//IOS8以下 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; //iOS8 - iOS10 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]]; //iOS10 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }
獲得用戶設置
iOS10 供給了獲得用戶受權相干設相信息的接口getNotificationSettingsWithCompletionHandler: , 回調帶有一個UNNotificationSettings對象,它具有以部屬性,可以精確獲得各類受權信息
authorizationStatus
soundSetting
badgeSetting
alertSetting
notificationCenterSetting
lockScreenSetting
carPlaySetting
alertStyle
像上面的辦法,點擊allow
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { //點擊許可 NSLog(@"注冊告訴勝利"); [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"%@", settings); }]; } else { //點擊不許可 NSLog(@"注冊告訴掉敗"); } }];
打印信息: *<UNNotificationSettings: 0x174090a90; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, alertSetting: NotSupported, carPlaySetting: Enabled, alertStyle: Banner>*
注冊APNS, 獲得token
iOS10, 注冊APNS和獲得token的辦法還和之前一樣
在application: didFinishLaunchingWithOptions:挪用 registerForRemoteNotifications辦法
[[UIApplication sharedApplication] registerForRemoteNotifications];
在署理辦法application: didRegisterForRemoteNotificationsWithDeviceToken:中獲得token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){ NSLog(@"deviceToken:%@",deviceToken); } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0){ NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error); }
設置處置告訴的action 和 category
在iOS8之前是沒有category這個屬性的;
在iOS8注冊推送,獲得受權的時刻,可以一並設置category, 注冊的辦法直接帶有這個參數;
在iOS10, 須要挪用一個辦法setNotificationCategories:來為治理推送的UNUserNotificationCenter實例設置category, category又可以對應設置action;
//設置category //UNNotificationActionOptionAuthenticationRequired 須要解鎖 //UNNotificationActionOptionDestructive 顯示為白色 //UNNotificationActionOptionForeground 點擊翻開app UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"戰略1行動1" options:UNNotificationActionOptionForeground]; UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action2" title:@"戰略1行動2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"comment" textInputPlaceholder:@"reply"]; //UNNotificationCategoryOptionNone //UNNotificationCategoryOptionCustomDismissAction 消除告訴被觸發會走告訴的署理辦法 //UNNotificationCategoryOptionAlloWinCarPlay 實用於行車形式 UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action3" title:@"戰略2行動1" options:UNNotificationActionOptionForeground]; UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action4" title:@"戰略2行動2" options:UNNotificationActionOptionForeground]; UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]];
設置告訴內容
由於iOS10長途告訴與當地告訴同一起來了,告訴內容屬性是分歧的,不外長途推送就須要在payload停止詳細設置了,上面以當地告訴為例,引見關於UNNotificationContent的內容
官網上明白解釋了,我們是不克不及直接創立UNNotificationContent的實例的, 假如我們須要本身去設置裝備擺設內容的各個屬性,我們須要用到UNMutableNotificationContent
看一下它的一些屬性:
attachments //附件
badge //徽標
body //推送內容body
categoryIdentifier //category標識
launchImageName //點擊告訴進入運用的啟動圖
sound //聲響
subtitle //推送內容子題目
title //推送內容題目
userInfo //長途告訴內容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"Test"; content.subtitle = @"1234567890"; content.body = @"Copyright © 2016年 jpush. All rights reserved."; content.badge = @1; NSError *error = nil; NSString *path = [[NSBundle mainBundle] pathForResource:@"718835727" ofType:@"png"]; UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error]; if (error) { NSLog(@"attachment error %@", error); } content.attachments = @[att]; content.categoryIdentifier = @"category1”; //這裡設置category1, 是與之前設置的category對應 content.launchImageName = @"1-Eb_0OvtcxJXHZ7-IOoBsaQ"; UNNotificationSound *sound = [UNNotificationSound defaultSound]; content.sound = sound;
告訴觸發器
UNNotificationTrigger
iOS 10觸發器有4種
•UNPushNotificationTrigger 觸發APNS辦事,體系主動設置(這是辨別當地告訴和長途告訴的標識)
•UNTimeIntervalNotificationTrigger 一段時光後觸發
•UNCalendarNotificationTrigger 指定日期觸發
•UNLocationNotificationTrigger 依據地位觸發,支撐進入某地或許分開某地或許都有
//十秒後 UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO]; //每周日早上8:00 NSDateComponents *component = [[NSDateComponents alloc] init]; component.weekday = 1; component.hour = 8; UNCalendarNotificationTrigger *trigger2 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:component repeats:YES]; //圓形區域,進入時刻停止告訴 CLLocationCoordinate2D cen = CLLocationCoordinate2DMake(80.335400, -90.009201); CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:cen radius:500.0 identifier:@“center"]; region.notifyOnEntry = YES; //進入的時刻 region.notifyOnExit = NO; //出去的時刻 UNLocationNotificationTrigger *trigger3 = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];
添加告訴 / 更新告訴
1.創立一個UNNotificationRequest類的實例,必定要為它設置identifier, 在前面的查找,更新, 刪除告訴,這個標識是可以用來辨別這個告訴與其他告訴
2.把request加到UNUserNotificationCenter, 並設置觸發器,期待觸發
3.
假如另外一個request具有和之前request雷同的標識,分歧的內容, 可以到達更新告訴的目標
NSString *requestIdentifer = @"TestRequest"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger1]; //把告訴加到UNUserNotificationCenter, 到指定觸發點會被觸發 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; //在別的須要更新告訴的處所 UNMutableNotificationContent *newContent = [[UNMutableNotificationContent alloc] init]; newContent.title = @"Update"; newContent.subtitle = @"XXXXXXXXX"; newContent.body = @"Copyright © 2016年 jpush. All rights reserved."; UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"TestRequest" content:newContent trigger:trigger1]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }];
獲得和刪除告訴
這裡告訴是有兩種狀況
•Pending 期待觸發的告訴
•Delivered 曾經觸成長示在告訴中間的告訴
//獲得未觸發的告訴 [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { NSLog(@"pending: %@", requests); }]; //獲得告訴中間列表的告訴 [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) { NSLog(@"Delivered: %@", notifications); }]; //消除某一個未觸發的告訴 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"TestRequest1"]]; //消除某一個告訴中間的告訴 [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest2"]]; //對應的刪除一切告訴 [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests]; [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
delegate
<UNUserNotificationCenterDelegate>
iOS10收到告訴不再是在application: didReceiveRemoteNotification:辦法行止理, iOS10推出新的署理辦法,吸收和處置各類告訴(當地或許長途)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { //運用在前台收到告訴 NSLog(@"========%@", notification); } - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { //點擊告訴進入運用 NSLog(@"response:%@", response); }
最初
下一篇文章持續引見關於富媒體推送的 UNNotificationServiceExtension 和 Notification content extension, 未完待續。。。
【iOS10 推送最新特征研討】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!