检查是否允许本地通知

我每天都在同一时间重复本地通知。

现在一切工作正常, 除非通知切换为OFF,然后重新打开,从OFF状态的日子开始build立通知。

有没有最好的方法来检查是否closures然后重新打开,并清除所有旧的通知?

谢谢!

- (void)viewDidLoad { [super viewDidLoad]; UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; // before we create any new ones, cancel all existing notifications [[UIApplication sharedApplication]cancelAllLocalNotifications]; UILocalNotification* localNotification = [[UILocalNotification alloc] init]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]]; comp.hour = 19; // 19 = 7PM comp.minute = 45; // 7:45 PM comp.second = 01; // 7:45:01 PM localNotification.fireDate = [cal dateFromComponents:comp]; localNotification.alertBody = @"Local Notification in iOS8"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitDay; // this will schedule the notification to fire at the fire date [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; // this will fire the notification right away, it will still also fire at the date we set [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; } 

在提交通知之前,请检查用户设置是否允许:

 - (void)viewDidLoad { [super viewDidLoad]; UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; // before we create any new ones, cancel all existing notifications [[UIApplication sharedApplication]cancelAllLocalNotifications]; if ([[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone) { UILocalNotification* localNotification = [[UILocalNotification alloc] init]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]]; comp.hour = 19; // 19 = 7PM comp.minute = 45; // 7:45 PM comp.second = 01; // 7:45:01 PM localNotification.fireDate = [cal dateFromComponents:comp]; localNotification.alertBody = @"Local Notification in iOS8"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitDay; // this will schedule the notification to fire at the fire date [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; // this will fire the notification right away, it will still also fire at the date we set [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; } } 

在切换事件,请尝试以下 –

 [[UIApplication sharedApplication] cancelAllLocalNotifications];