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

标签 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];


}
  }

我哪里做错了?请帮忙

提前致谢!!

最佳答案

你检查了[prefs stringForKey:@"Notification"],但是你还没有定义prefs所以它可能是nil,而且你从来没有设置值对于 Notification 这样也可能会丢失。任何这些都意味着您在不期望的时候添加。

您正在调用 scheduleLocalNotification: 但您没有在任何地方调用 cancelLocalNotification:cancelAllLocalNotifications 因此,根据用户使用应用程序的方式,或者您如何进行测试,您可以轻松地同时添加多个通知。使用 scheduledLocalNotifications 检查当前安排的内容。

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

关于ios - 本地通知在错误的时间发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30185487/

相关文章:

ios - if/and 语句中使用 swift for iOS 的奇怪行为

ios - 解析 Rest API - 带有高级定位错误的推送通知

ios - 从 CFCoverFlowView 推送 View Controller

ios - 我的 swift 代码不会执行振动

javascript - 焦点到 iPhone 上文本区域的开头?

ios - 自动布局中的红色中断线是什么意思?

ios - UIACollectionView 单元格与 visibleCells

ios - tableView viewForHeaderInSection 从 ios7 中的第 1 节开始

ios - redirect_uri 的参数值无效

iphone - Xcode 4 从早期提交恢复文件