0

我编写了预定的 10 个 UILocalNotification,但只有 6 个被解雇了。这很奇怪。如果我安排了 5 个,只有 3 个被解雇了。有人可以帮帮我吗?提前致谢。以下是处理调度的方法。

- (void)scheduleLocalNotifications
{
    // Get the current datetime
    NSDate *current = [NSDate date];

    // Get the intervals
    float workSessionInterval1 = (isWorking) ? (float)currentSessionTimer : (float)currentSessionTimer+(float)workingTimeSpan;
    float restSessionInterval1 = (isWorking) ? workSessionInterval1+(float)restTimeSpan : (float)currentSessionTimer;

    float workSessionInterval2 = restSessionInterval1 + (float)workingTimeSpan;
    float restSessionInterval2 = workSessionInterval1 + (float)restTimeSpan;

    float workSessionInterval3 = restSessionInterval2 + (float)workingTimeSpan;
    float restSessionInterval3 = workSessionInterval2 + (float)restTimeSpan;

    float workSessionInterval4 = restSessionInterval3 + (float)workingTimeSpan;
    float restSessionInterval4 = workSessionInterval3 + (float)restTimeSpan;

    float workSessionInterval5 = restSessionInterval4 + (float)workingTimeSpan;
    float restSessionInterval5 = workSessionInterval4 + (float)restTimeSpan;

    // Schedule working session ends notifications
    UILocalNotification *workSessionEndNotif1 = [[UILocalNotification alloc] init];
    workSessionEndNotif1.fireDate = [current dateByAddingTimeInterval:workSessionInterval1];
    workSessionEndNotif1.timeZone = [NSTimeZone defaultTimeZone];
    workSessionEndNotif1.alertBody = @"You working session finishes. Go to have a rest";
    workSessionEndNotif1.alertAction = @"View";
    workSessionEndNotif1.soundName = UILocalNotificationDefaultSoundName;
    workSessionEndNotif1.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:workSessionEndNotif1];
    workSessionEndNotif1 = nil;

    UILocalNotification *workSessionEndNotif2 = [[UILocalNotification alloc] init];
    workSessionEndNotif2.fireDate = [current dateByAddingTimeInterval:workSessionInterval2];
    workSessionEndNotif2.timeZone = [NSTimeZone defaultTimeZone];
    workSessionEndNotif2.alertBody = @"You working session finishes. Go to have a rest";
    workSessionEndNotif2.alertAction = @"View";
    workSessionEndNotif2.soundName = UILocalNotificationDefaultSoundName;
    workSessionEndNotif2.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:workSessionEndNotif2];
    workSessionEndNotif2 = nil;

    UILocalNotification *workSessionEndNotif3 = [[UILocalNotification alloc] init];
    workSessionEndNotif3.fireDate = [current dateByAddingTimeInterval:workSessionInterval3];
    workSessionEndNotif3.timeZone = [NSTimeZone defaultTimeZone];
    workSessionEndNotif3.alertBody = @"You working session finishes. Go to have a rest";
    workSessionEndNotif3.alertAction = @"View";
    workSessionEndNotif3.soundName = UILocalNotificationDefaultSoundName;
    workSessionEndNotif3.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:workSessionEndNotif3];
    workSessionEndNotif3 = nil;

    UILocalNotification *workSessionEndNotif4 = [[UILocalNotification alloc] init];
    workSessionEndNotif4.fireDate = [current dateByAddingTimeInterval:workSessionInterval4];
    workSessionEndNotif4.timeZone = [NSTimeZone defaultTimeZone];
    workSessionEndNotif4.alertBody = @"You working session finishes. Go to have a rest";
    workSessionEndNotif4.alertAction = @"View";
    workSessionEndNotif4.soundName = UILocalNotificationDefaultSoundName;
    workSessionEndNotif4.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:workSessionEndNotif4];
    workSessionEndNotif4 = nil;

    UILocalNotification *workSessionEndNotif5 = [[UILocalNotification alloc] init];
    workSessionEndNotif5.fireDate = [current dateByAddingTimeInterval:workSessionInterval5];
    workSessionEndNotif5.timeZone = [NSTimeZone defaultTimeZone];
    workSessionEndNotif5.alertBody = @"You working session finishes. Go to have a rest";
    workSessionEndNotif5.alertAction = @"View";
    workSessionEndNotif5.soundName = UILocalNotificationDefaultSoundName;
    workSessionEndNotif5.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:workSessionEndNotif5];
    workSessionEndNotif5 = nil;

    // Schedule rest session ends notifications
    UILocalNotification *restSessionEndNotif1 = [[UILocalNotification alloc] init];
    restSessionEndNotif1.fireDate = [current dateByAddingTimeInterval:restSessionInterval1];
    restSessionEndNotif1.timeZone = [NSTimeZone defaultTimeZone];
    restSessionEndNotif1.alertBody = @"You rest session finishes. Go to work";
    restSessionEndNotif1.alertAction = @"View";
    restSessionEndNotif1.soundName = UILocalNotificationDefaultSoundName;
    restSessionEndNotif1.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:restSessionEndNotif1];
    restSessionEndNotif1 = nil;

    UILocalNotification *restSessionEndNotif2 = [[UILocalNotification alloc] init];
    restSessionEndNotif2.fireDate = [current dateByAddingTimeInterval:restSessionInterval2];
    restSessionEndNotif2.timeZone = [NSTimeZone defaultTimeZone];
    restSessionEndNotif2.alertBody = @"You rest session finishes. Go to work";
    restSessionEndNotif2.alertAction = @"View";
    restSessionEndNotif2.soundName = UILocalNotificationDefaultSoundName;
    restSessionEndNotif2.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:restSessionEndNotif2];
    restSessionEndNotif2 = nil;

    UILocalNotification *restSessionEndNotif3 = [[UILocalNotification alloc] init];
    restSessionEndNotif3.fireDate = [current dateByAddingTimeInterval:restSessionInterval3];
    restSessionEndNotif3.timeZone = [NSTimeZone defaultTimeZone];
    restSessionEndNotif3.alertBody = @"You rest session finishes. Go to work";
    restSessionEndNotif3.alertAction = @"View";
    restSessionEndNotif3.soundName = UILocalNotificationDefaultSoundName;
    restSessionEndNotif3.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:restSessionEndNotif3];
    restSessionEndNotif3 = nil;

    UILocalNotification *restSessionEndNotif4 = [[UILocalNotification alloc] init];
    restSessionEndNotif4.fireDate = [current dateByAddingTimeInterval:restSessionInterval4];
    restSessionEndNotif4.timeZone = [NSTimeZone defaultTimeZone];
    restSessionEndNotif4.alertBody = @"You rest session finishes. Go to work";
    restSessionEndNotif4.alertAction = @"View";
    restSessionEndNotif4.soundName = UILocalNotificationDefaultSoundName;
    restSessionEndNotif4.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:restSessionEndNotif4];
    restSessionEndNotif4 = nil;

    UILocalNotification *restSessionEndNotif5 = [[UILocalNotification alloc] init];
    restSessionEndNotif5.fireDate = [current dateByAddingTimeInterval:restSessionInterval5];
    restSessionEndNotif5.timeZone = [NSTimeZone defaultTimeZone];
    restSessionEndNotif5.alertBody = @"You rest session finishes. Go to work";
    restSessionEndNotif5.alertAction = @"View";
    restSessionEndNotif5.soundName = UILocalNotificationDefaultSoundName;
    restSessionEndNotif5.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:restSessionEndNotif5];
    restSessionEndNotif5 = nil;
}
4

1 回答 1

0

我记录了每个通知将触发的时间,结果发现其中只有 6 个会在不同的时间触发。所以我只收到了 6 个通知,因为有些是同时安排的。

事实证明,我没有正确安排时间。

于 2013-08-15T03:28:52.670 回答