本地通知在错误的时间发送

时间:2015-05-12 08:24:28

标签: ios objective-c iphone ipad uilocalnotification

我想在iOS的特定时间每天发送2个本地通知

但是通知是在错误的时间发送的,并且它在一天内发送多次,而不是一次,

这是我的代码段,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 // are you running on iOS8?
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else // iOS 7 or earlier
    {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    application.applicationIconBadgeNumber = 0;



      //This if will be called only once....
     if ([prefs stringForKey:@"Notification"] == nil) 
     {


 //... 1st notification ...

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
    [components setHour:11];
    [components setMinute:24];

    // Gives us today's date but at 11am
    NSDate *next11am = [calendar dateFromComponents:components];
    if ([next11am timeIntervalSinceNow] < 0) {
        // If today's 9am already occurred, add 24hours to get to tomorrow's
        next11am = [next11am dateByAddingTimeInterval:60*60*24];
    }

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = next11am;
    notification.alertBody = @"Notification 1.";
    // Set a repeat interval to daily
    notification.repeatInterval = NSDayCalendarUnit;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];




//... 2nd Notification ...

    NSDate *now1 = [NSDate date];
    NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components1 = [calendar1 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now1];
    [components1 setHour:18];
    [components1 setMinute:30];

    // Gives us today's date but at 9am
    NSDate *next6pm = [calendar dateFromComponents:components];
    if ([next6pm timeIntervalSinceNow] < 0) {
        // If today's 6pm already occurred, add 24hours to get to tomorrow's
        next6pm = [next6pm dateByAddingTimeInterval:60*60*24];
    }

    UILocalNotification *notification1 = [[UILocalNotification alloc] init];
    notification1.fireDate = next6pm;
    notification1.alertBody = @"Notification 2.";


    // Set a repeat interval to daily
    notification1.repeatInterval = NSDayCalendarUnit;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification1];


}
  }

我在哪里做错了?请帮助

提前致谢!!

1 个答案:

答案 0 :(得分:2)

您检查[prefs stringForKey:@"Notification"],但尚未定义prefs因此它可能为零,并且您似乎永远不会设置Notification的值,因此也可能会丢失。任何这些事情都意味着你在不期望的时候加入。

您正在拨打scheduleLocalNotification:,但您无法呼叫cancelLocalNotification:cancelAllLocalNotifications,具体取决于用户使用该应用的方式,或您是如何进行的测试时,您可以轻松地同时添加多个通知。使用scheduledLocalNotifications检查当前安排的内容。

您可能还应该使用currentCalendar作为日历,因为您需要尊重用户设置。