9

我想要每分钟随机的本地通知(文本和声音)。我正在使用以下代码:

self.randomIndex_text  = arc4random() % [self.array_motivation count];
self.randomIndex_alarm = arc4random() % [self.array_alarm count];
NSLog(@"text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm);

此代码非常适用于

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
notif.soundName = [NSString stringWithFormat:@"%@.mp3", [self.array_alarm objectAtIndex:self.randomIndex_alarm]];
    [self _showAlert:[NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]] withTitle:@"Daily Achiever"];
}

从上面的代码显示警报,并在警报确定时显示以下方法调用:

-(void)insert:(NSDate *)fire
{
    self.localNotification = [[UILocalNotification alloc] init];

    if (self.localNotification == nil)
        return;

    self.randomIndex_text  = arc4random() % [self.array_motivation count];
    self.randomIndex_alarm = arc4random() % [self.array_alarm count];
    NSLog(@"text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm);

    self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:refTimeIntrval];
    self.localNotification.timeZone = [NSTimeZone defaultTimeZone];
    self.localNotification.alertBody = [NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]];
    self.localNotification.soundName = [NSString stringWithFormat:@"%@.mp3",[self.array_alarm objectAtIndex:self.randomIndex_alarm]];
    self.localNotification.alertAction = @"View";
    self.localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;
    self.localNotification.repeatInterval=NSMinuteCalendarUnit;

    NSLog(@"alertBody %@,soundName %@", self.localNotification.alertBody, self.localNotification.soundName);
    [[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
}

但在后台不起作用。我只是把这个上面的随机方法放在

- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);
    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{
        while ([application backgroundTimeRemaining] > 1.0)
        {
            UILocalNotification *localNotif = [[UILocalNotification alloc] init];
            if (localNotif)
            {
                self.randomIndex_text  = arc4random() % [self.array_motivation count];
                self.randomIndex_alarm = arc4random() % [self.array_alarm count];
                NSLog(@"tempmethod text %d, alarm %d",self.randomIndex_text, self.randomIndex_alarm);

                localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:refTimeIntrval];
                localNotif.alertBody = [NSString stringWithFormat:@"%@",[self.array_motivation objectAtIndex:self.randomIndex_text]];
                localNotif.soundName =[NSString stringWithFormat:@"%@.mp3",[self.array_alarm objectAtIndex:self.randomIndex_alarm]];
                localNotif.alertAction = NSLocalizedString(@"Read Msg", nil);
                localNotif.applicationIconBadgeNumber = 1;
                [localNotif setRepeatInterval:NSMinuteCalendarUnit];
                [application presentLocalNotificationNow:localNotif];

                NSLog(@"sound: %@, alertAction: %@, alerBody: %@, ref: %f, str_time: %@",localNotif.soundName, localNotif.alertAction, localNotif.alertBody, refTimeIntrval, str_Time);

                [self performSelector:@selector(bgmethodd) withObject:nil afterDelay:refTimeIntrval];
                break;
            }
        }
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;
    });
    NSLog(@"smh: %d,%d,%d",self.seconds, self.minutes, self.hours);
    }
}

当我applicationDidEnterBackground只调试一次调用时(即应用程序在后台移动时),我注意到的另一件事。之后没有任何方法调用,直到应用程序再次打开,但我仍然收到通知文本和声音连续。但是这个文字和声音不是随机的。

请建议我一些想法并分享您的知识,即当后台没有任何方法调用时,此通知文本和声音来自何处。是否可以在后台随机通知。提前致谢。

4

1 回答 1

2

当应用程序进入后台时,您安排的第一个通知以间隔 NSMinuteCalendarUnit 重复,因此应用程序每分钟仅显示该通知。

为了获得随机警报和声音,本地通知需要在后台执行一些代码来生成下一个随机声音和警报,这是不可能的。

一种方法是提前安排 64 个(最多)本地通知,其中包含随机声音和警报。当用户打开应用程序时,您可以查看在后台触发了多少通知,并重新安排它们。

为了确保即使用户在这 64 个通知期间没有打开应用程序也会触发本地通知,最后一个通知需要以间隔 NSMinuteCalendarUnit 重复。所以在前 63 次通知之后,你会失去随机性,但如果用户经常打开应用程序,这不会有问题。

于 2014-08-24T11:38:14.043 回答