gpt4 book ai didi

iphone - 后台随机本地通知

转载 作者:可可西里 更新时间:2023-11-01 02:59:01 24 4
gpt4 key购买 nike

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

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 调用时,我还注意到了另一件事(即当应用程序在后台移动时)。在那之后没有任何方法调用,直到应用程序再次打开,但我仍然收到通知文本和声音 continoulsy。但是这个文本和声音不是随机的。

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

最佳答案

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

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

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

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

关于iphone - 后台随机本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15874875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com