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

Posted

技术标签:

【中文标题】本地通知在错误的时间发送【英文标题】:local notification are sent at wrong time 【发布时间】:2015-05-12 08:24:27 【问题描述】:

我想在 ios 的特定时间每天发送 2 条本地通知,

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

这是我的代码 sn-p,

- (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】:

您检查了[prefs stringForKey:@"Notification"],但您尚未定义prefs,因此它可能为nil,并且您似乎从未设置Notification 的值,因此它也可能丢失。任何这些都意味着您在不希望添加时添加。

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

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

【讨论】:

在我看来日期逻辑也是错误的。我是否正确地阅读它正在创建一个日期,给定一个时间,创建一个没有时间的新日期,但 OP 期望这两个日期具有相同的时间? @SimonMcLoughlin 看起来确实在计算和设置的 11 / 6 / 9 倍之间存在一些混淆。更详细地看,创建了 2 次但未实际使用,而是将两个通知都设置为 next9am 未定义... 查看我的新编辑@Wain:我在代码中定义了prefs,我没有在此处添加不必要的代码,如果条件我在那里写了这个//This if will be called only once....,当用户转身时有一个开关我已经写了//Notification turned Off [[UIApplication sharedApplication] cancelAllLocalNotifications]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 好的,它现在做什么?另外,请注意您不应该使用dateByAddingTimeInterval,您应该在组件中添加一天并生成一个新日期... 仍然存在同样的问题,当我记录我的预定通知时,它显示了许多通知。喜欢&lt;UIConcreteLocalNotification: 0x7f9e19c77920&gt;fire date = Thursday, May 7, 2015 at 12:35:47 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = NSCalendarUnitDay, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday, May 13, 2015 at 12:35:47 PM India Standard Time, user info = (null)

以上是关于本地通知在错误的时间发送的主要内容,如果未能解决你的问题,请参考以下文章

为啥我收到远程通知时没有发送本地通知?

如何离线发送通知,而不是本地通知?

iOS 在收到静默远程通知后发送本地通知

本地通知不会出现 - iOS

后台运行的应用程序发送本地通知

iOS 13本地通知未发送