我在我的应用程序中有大约10个本地通知(不在数组中)及其在交换机中。我用过这段代码
NSUserDefaults *defaultsB = [NSUserDefaults standardUserDefaults];
UILocalNotification *notification = [[UILocalNotification alloc]init];
if (switcher.on == 1) {
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
[dateComponent setWeekday:1]; // For sunday
[dateComponent setHour:[timeHClass.text integerValue]];
[dateComponent setMinute:[timeMClass.text integerValue]];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
[notification setAlertBody:textClass1.text];
[notification setFireDate:fireDate];
notification.soundName = @"bells.mp3";
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[defaultsB setObject:@"ON" forKey:@"SwitchState"];
}
else {
UIApplication *app=[UIApplication sharedApplication];
[app cancelLocalNotification:notification];
[defaultsB setObject:@"OFF" forKey:@"SwitchState"];
}
但是当我关闭时,通知仍在工作?
我应该保存NSUserDefaults中的通知保存而不仅仅是交换机??
如果是的话怎么样?
答案 0 :(得分:2)
如果您想要取消现有通知,您需要通过[[UIApplication sharedApplication] scheduledLocalNotifications]
获取对它的引用,然后循环浏览这些通知并找到您想要的通知(可能使用您在创建期间给出的唯一标识符)。 / p>
或者,如果您要取消所有通知,请替换:
[app cancelLocalNotification:notification];
使用:
[app cancelAllLocalNotifications];