我正在安排UILocalNotifications。它们存储并按预期工作。但是一旦我安排了更多的UILocalNotification,之前创建的那些就会丢失。并且,从Xcode 6启动应用程序之间的所有通知都会丢失。
我有2个类来管理不同的通知(创建,取消等)
这是第一个,另一个非常类似,错误出现在:
static NSCalendar *currentCalendar;
@implementation PBWurmProphylaxeNotificationsManager
+ (void)initialize{
currentCalendar = [NSCalendar currentCalendar];
}
+(void)createAndScheduleNotificationWith:(NSDate *)fireDate withAlertBody:(NSString *)alertBody withUserInfo:(NSDictionary *)userInfo{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.repeatInterval = NSYearCalendarUnit;
notification.alertBody = alertBody;
notification.hasAction = NO;
notification.userInfo = userInfo;
notification.timeZone = [NSTimeZone localTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
+(void)enableWurmProphylaxeNotifications:(int)count{
// loop over the dogs array
NSArray *hunde = [self fetchHunde];
for (Hund *myDog in hunde) {
[self enable:count WurmProphylaxeNotificationsForHund:myDog];
}
NSLog(@"[[[UIApplication sharedApplication] scheduledLocalNotifications] count] %lu", (unsigned long)[[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
}
+(void)enable:(int)count WurmProphylaxeNotificationsForHund:(Hund *)myDog{
NSDate *day = [NSDate date];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
int loops;
switch (count) {
case 2:
// schedule 6 notifications with time difference of 2 month and yearly repeatInterval
[myComps setMonth:2];
loops = 6;
break;
case 4:
// schedule 3 notifications with time difference of 4 month and yearly repeatInterval
[myComps setMonth:4];
loops = 3;
break;
case 6:
// schedule 2 notifications with time difference of 6 month and yearly repeatInterval
[myComps setMonth:6];
loops = 2;
break;
default:
// schedule 3 notifications with time difference of 4 month and yearly repeatInterval
[myComps setMonth:4];
loops = 3;
break;
}
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
[userInfo setValue:myDog.uuid forKey:KEY_DOG_UUID];
[userInfo setValue:@YES forKey:IS_WURM_PROPHYLAXE_NOTIFICATION];
NSString *alertBody = [NSString stringWithFormat:@"Wurmprophylaxe für %@ fällig.", myDog.name];
for (int i = 0; i < loops; i++) {
day = [currentCalendar dateByAddingComponents:myComps toDate:day options:0];
[self createAndScheduleNotificationWith:day withAlertBody:alertBody withUserInfo:userInfo];
}
}
+(void)disableWurmProphylaxeNotifications{
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *notification in scheduledNotifications) {
NSDictionary *userInfo = notification.userInfo;
if([userInfo valueForKey:IS_WURM_PROPHYLAXE_NOTIFICATION]){
notification.userInfo = nil;
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
NSLog(@"[[[UIApplication sharedApplication] scheduledLocalNotifications] count] %lu", (unsigned long)[[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
}
+(void)disableWurmProphylaxeNotificationsForHund:(Hund *)myDog{
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];for (UILocalNotification *notification in scheduledNotifications) {
NSDictionary *userInfo = notification.userInfo;
if([userInfo valueForKey:IS_WURM_PROPHYLAXE_NOTIFICATION]){
NSString *uuid = (NSString *)[userInfo valueForKey:KEY_DOG_UUID];
if ([uuid isEqualToString:myDog.uuid]) {
notification.userInfo = nil;
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
}
+(NSArray *)fetchHunde{
CoreDataHelper *cdh = [(AppDelegate *) [[UIApplication sharedApplication] delegate] cdh];
NSManagedObjectContext *context = [cdh context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Hund" inManagedObjectContext:[cdh context]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(uuid != %@)", @"dog_unbekannt"];
[fetchRequest setPredicate:pred];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort1, nil]];
[fetchRequest setFetchBatchSize:20];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:fetchRequest error:&error];
return results;
}
@end
以下是调用上述类方法的代码:
// schedule notification if active
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:WURM_PROPHYLAXE_ERINNERN]) {
// schedule the notification as per user settings
int count = [defaults integerForKey:KEY_TEST];
[PBWurmProphylaxeNotificationsManager enable:count WurmProphylaxeNotificationsForHund:myDog];
}
出于调试目的,我使用的是表视图控制器,它显示了这个数组:
_scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
有什么问题?我怎样才能有效地调试本地通知?