UILocalNotification
已经在 iOS10 中被废弃了。需要使用 UserNotifications.Framework
来推送通知。
获取权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions UNUserNotificationCenter *unCenter = [UNUserNotificationCenter currentNotificationCenter]; unCenter.delegate = self; [unCenter requestAuthorizationWithOptions:UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; }
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { completionHandler(); }
|
注册本地通知
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
| + (void)registerLocalNotification { UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"通知标题"; content.body = @"通知内容"; content.sound = UNNotificationSound.defaultSound; content.sound = [UNNotificationSound soundNamed:@"sound.wav"]; content.badge = @1; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO]; NSString *identifier = @"notification_id"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { NSLog(@"registerLocalPush error: %@", error); }]; }
+ (void)removeLocalNotification:(NSString*)identifier { UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center removePendingNotificationRequestsWithIdentifiers:@[identifier]]; }
+ (void)removeAllLocalNotifications { UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllDeliveredNotifications]; [center removeAllPendingNotificationRequests]; }
|
参考链接