官方將通知單獨放在了UserNotifications.framework,使用時需要導入框架。UserNotifications.framework主要類文件: UNUserNotificationCenter的應用: 請求用戶授權: UNUserNotificationCenter* cente ...
官方將通知單獨放在了UserNotifications.framework,使用時需要導入框架。
UserNotifications.framework主要類文件:
UNCalendarNotificationTrigger
UNLocationNotificationTrigger
UNMutableNotificationContent
UNNotification
UNNotificationAction
UNNotificationAttachment
UNNotificationCategory
UNNotificationContent
UNNotificationRequest
UNNotificationResponse
UNNotificationServiceExtension
UNNotificationSettings
UNNotificationSound
UNNotificationTrigger
UNPushNotificationTrigger
UNTextInputNotificationAction
UNTextInputNotificationResponse
UNTimeIntervalNotificationTrigger
UNUserNotificationCenter
UNUserNotificationCenter的應用:
- 請求用戶授權:
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; // 請求授權 /* UNAuthorizationOptionBadge = (1 << 0), UNAuthorizationOptionSound = (1 << 1), UNAuthorizationOptionAlert = (1 << 2), UNAuthorizationOptionCarPlay = (1 << 3), */ [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if(granted){
if(granded)
//同意
}else{
//不同意
}}];
補充:
獲取授權設置信息
// 獲取通知授權和設置 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { /* UNAuthorizationStatusNotDetermined : 沒有做出選擇 UNAuthorizationStatusDenied : 用戶未授權 UNAuthorizationStatusAuthorized :用戶已授權 */ if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) { NSLog(@"未選擇"); }else if (settings.authorizationStatus == UNAuthorizationStatusDenied){ NSLog(@"未授權"); }else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized){ NSLog(@"已授權"); } }]
-
創建本地通知:
// 創建一個本地通知 UNMutableNotificationContent *content_1 = [[UNMutableNotificationContent alloc] init]; // 主標題 content_1.title = [NSString localizedUserNotificationStringForKey:@"title" arguments:nil]; // 副標題 content_1.subtitle = [NSString localizedUserNotificationStringForKey:@"subtitle" arguments:nil]; content_1.badge = [NSNumber numberWithInteger:1]; content_1.body = [NSString localizedUserNotificationStringForKey:@"title_message_for_yan" arguments:nil]; content_1.sound = [UNNotificationSound defaultSound]; // 設置觸發時間 UNTimeIntervalNotificationTrigger *trigger_1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO]; // 創建一個發送請求 UNNotificationRequest *request_1 = [UNNotificationRequest requestWithIdentifier:@"my_notification" content:content_1 trigger:trigger_1];
補充:
- UserNotifications提供了三種觸發器:
UNTimeIntervalNotificationTrigger :一定時間後觸發 UNCalendarNotificationTrigger : 在某月某日某時觸發 UNLocationNotificationTrigger : 在用戶進入或是離開某個區域時觸發
- @“my_notification”請求的標識符可以用來管理通知:
- 移除還未展示的通知 [center removePendingNotificationRequestsWithIdentifiers: @[@“my_notification”]]; [center removeAllPendingNotificationRequests]; // - (void)cancelAllLocalNotifications; - 移除已經展示過的通知 [center removeDeliveredNotificationsWithIdentifiers:@[@“my_notification”]]; [center removeAllDeliveredNotifications]; - 獲取未展示的通知 [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { NSLog(@"%@",requests); }]; - 獲取展示過的通知 [center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) { NSLog(@"%@",notifications); }];
- 遠程通知的格式:
{ "aps":{ "alert":{ "title":"I am title", "subtitle":"I am subtitle", "body":"I am body" }, "sound":"default", "badge":1 } }
具體請參考官方文檔
- UserNotifications提供了三種觸發器:
-
將通知請求添加到通知中心(UNUserNotificationCenter):
[center addNotificationRequest:request_1 withCompletionHandler:^(NSError * _Nullable error) {
}];
接收通知
-
處理通知:
設置UNUserNotificationCenterDelegate:註意:
UNUserNotificationCenter 的 delegate 必須在 application:willFinishLaunchingWithOptions: orapplication:didFinishLaunchingWithOptions: 方法中實現;center.delegate = self;
-
應用內展示通知:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ // 如果不想顯示某個通知,可以直接用空 options 調用 completionHandler: // completionHandler([]) completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound); }
- 在用戶與你推送的通知進行交互時被調用:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ completionHandler(); NSLog(@"userInfo--%@",response.notification.request.content.userInfo); }
-
UNNotificationCategory的應用:
- 創建一個 category:
/* UNNotificationActionOptionAuthenticationRequired = (1 << 0), UNNotificationActionOptionDestructive = (1 << 1), 取消 UNNotificationActionOptionForeground = (1 << 2), 啟動程式 */ UNTextInputNotificationAction *textAction = [UNTextInputNotificationAction actionWithIdentifier:@"my_text" title:@"text_action" options:UNNotificationActionOptionForeground textInputButtonTitle:@"輸入" textInputPlaceholder:@"預設文字"]; UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"my_action" title:@"action" options:UNNotificationActionOptionDestructive]; UNNotificationAction *action_1 = [UNNotificationAction actionWithIdentifier:@"my_action_1" title:@"action_1" options:UNNotificationActionOptionAuthenticationRequired]; /* UNNotificationCategoryOptionNone = (0), UNNotificationCategoryOptionCustomDismissAction = (1 << 0), UNNotificationCategoryOptionAllowInCarPlay = (2 << 0), */ UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"my_category" actions:@[textAction,action,action_1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; NSSet *setting = [NSSet setWithObjects:category, nil]; [center setNotificationCategories:setting];
- 在創建 UNNotificationContent 時把 categoryIdentifier 設置為需要的 category id 即可:
content.categoryIdentifier = @"my_category";
遠程推送也可以使用 category,只需要在 payload 中添加 category 欄位,並指定預先定義的 category id 就可以了
- 處理category的通知:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ completionHandler(); NSLog(@"userInfo--%@",response.notification.request.content.userInfo); // 獲取通知中心的所有的Categories [center getNotificationCategoriesWithCompletionHandler:^(NSSet<UNNotificationCategory *> * _Nonnull categories) { for (UNNotificationCategory *category in categories) { if ([category.identifier isEqualToString:@"my_category"] && [response.notification.request.content.categoryIdentifier isEqualToString:@"my_category"]) { for (UNNotificationAction *textAction in category.actions) { if ([textAction.identifier isEqualToString:@"my_text"]) { UNTextInputNotificationAction *text = (UNTextInputNotificationAction *)textAction; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:text.textInputButtonTitle preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:nil]]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil]; } } } } }]; }
長按 3D touch 效果圖
進入應用
iOS 10 中被標為棄用的 API
UILocalNotification
UIMutableUserNotificationAction
UIMutableUserNotificationCategory
UIUserNotificationAction
UIUserNotificationCategory
UIUserNotificationSettings
handleActionWithIdentifier:forLocalNotification:
handleActionWithIdentifier:forRemoteNotification:
didReceiveLocalNotification:withCompletion:
didReceiveRemoteNotification:withCompletion: