iOS10 废弃了 UILocalNotification
,采用了新的UserNotifications Framework
来推送通知。
注册推送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #import <UserNotifications/UserNotifications.h> #import "AppDelegate.h" @interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { }]; } return YES; }
@end
|
使用本地通知
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| - (void)showLocalNotificationWithTitle:(NSString *)title notificationId:(NSString *)notificationId { if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removePendingNotificationRequestsWithIdentifiers:@[notificationId]]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = [NSString localizedUserNotificationStringForKey:title arguments:nil]; content.userInfo = @{kClickLocalNotification: notificationId}; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:notificationId content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:nil]; } else { [self deleteLocadNotificationWithNotificationId:notificationId]; UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.repeatInterval = kCFCalendarUnitEra; notification.alertBody = title; notification.applicationIconBadgeNumber = 0; notification.soundName = UILocalNotificationDefaultSoundName; notification.userInfo = @{kClickLocalNotification: notificationId}; if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else { } [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } }
- (void)deleteLocadNotificationWithNotificationId:(NSString *)notificationId { if (@available(iOS 10.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]]; } else { UILocalNotification * notification = [self queryNotificationWithNotificatioId:notificationId]; if (notification) { [[UIApplication sharedApplication] cancelLocalNotification:notification]; } } }
- (UILocalNotification *)queryNotificationWithNotificatioId:(NSString *)notificatioId { NSArray * notifications = [self queryAllSystemNotifications]; __block UILocalNotification * localnotification = nil; if (notifications.count > 0) { [notifications enumerateObjectsUsingBlock:^(UILocalNotification * obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.userInfo containsObjectForKey:kClickLocalNotification] && [obj.userInfo[kClickLocalNotification] isEqualToString:notificatioId]) { localnotification = obj; *stop = YES; } }]; } return localnotification; }
- (NSArray *)queryAllSystemNotifications { return [[UIApplication sharedApplication] scheduledLocalNotifications]; }
|
1 2
| getPendingNotificationRequestsWithCompletionHandler: getDeliveredNotificationsWithCompletionHandler:
|