我每天都在同一时间重复本地通知。
现在一切正常,除非通知切换为关闭,然后重新打开,从关闭的日期开始会收到通知。
是否有最好的方法来检查是否切换为OFF然后重新打开,并清除所有旧通知?
谢谢!
- (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];
}
答案 0 :(得分:1)
在提交通知之前,检查用户设置是否允许:
- (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];
}
}
答案 1 :(得分:-2)
在事件上切换时,请尝试以下操作 -
[[UIApplication sharedApplication] cancelAllLocalNotifications];