Ios 以前的UILocalNotification在计划新通知时以及在应用程序启动之间丢失
我正在安排UILocalNotifications。它们按预期存储和工作。但一旦我安排了更多的UILocalNotification,以前创建的那些就丢失了。而且,在从Xcode 6启动应用程序之间,所有通知都会丢失 我有两个类管理不同的通知(创建、取消等) 这是第一个,另一个非常相似,错误出现在两个方面:Ios 以前的UILocalNotification在计划新通知时以及在应用程序启动之间丢失,ios,iphone,uilocalnotification,Ios,Iphone,Uilocalnotification,我正在安排UILocalNotifications。它们按预期存储和工作。但一旦我安排了更多的UILocalNotification,以前创建的那些就丢失了。而且,在从Xcode 6启动应用程序之间,所有通知都会丢失 我有两个类管理不同的通知(创建、取消等) 这是第一个,另一个非常相似,错误出现在两个方面: static NSCalendar *currentCalendar; @implementation PBWurmProphylaxeNotificationsManager + (v
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
出于调试目的,我正在使用显示此数组的表视图控制器:
_scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
怎么了?如何有效地调试本地通知
_scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];