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

local notification are sent at wrong time

我想在 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];


}
  }

我哪里做错了?请帮忙

提前致谢!!

你检查了 [prefs stringForKey:@"Notification"],但你没有定义 prefs 所以它可能是 nil,而且你似乎从来没有设置 Notification 的值所以它也可能丢失.这些事情中的任何一个都意味着你在你不期望的时候添加。

你正在调用 scheduleLocalNotification: 但你没有调用 cancelLocalNotification:cancelAllLocalNotifications 所以,这取决于用户如何使用应用程序,或者你如何进行测试,您可以轻松地同时添加多个通知。使用 scheduledLocalNotifications 查看当前安排的内容。

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