Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILocationNotification.fireDate在过去-bug?_Ios_Cocoa Touch_Uilocalnotification - Fatal编程技术网

Ios UILocationNotification.fireDate在过去-bug?

Ios UILocationNotification.fireDate在过去-bug?,ios,cocoa-touch,uilocalnotification,Ios,Cocoa Touch,Uilocalnotification,根据onUILocationNotification.fireDate: 如果指定的值为零或是过去的日期,则 通知立即送达 在过去使用日期时,我没有看到这种行为。是我一个人,还是其他人也看到了这一点 这是我的密码: NSMutableArray *notifications = [NSMutableArray array]; UILocalNotification* alarm = [[UILocalNotification alloc] init]; alarm.fireDate = [NSD

根据on
UILocationNotification.fireDate

如果指定的值为零或是过去的日期,则 通知立即送达

在过去使用日期时,我没有看到这种行为。是我一个人,还是其他人也看到了这一点

这是我的密码:

NSMutableArray *notifications = [NSMutableArray array];
UILocalNotification* alarm = [[UILocalNotification alloc] init];
alarm.fireDate = [NSDate dateWithTimeIntervalSince1970:time(NULL)-5];
alarm.repeatInterval = 0;
alarm.soundName = @"alarm.caf";
alarm.alertBody = @"Test";
alarm.alertAction = @"Launch";
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
[userInfo setValue:[NSNumber numberWithInt:10] forKey:@"PsID"];
alarm.userInfo = userInfo;
notifications = [NSArray arrayWithObject:alarm];
UIApplication *app = [UIApplication sharedApplication];
app.scheduledLocalNotifications = notifications;
如果我将时间(NULL)-5更改为时间(NULL)+5,我会在代码运行5秒后收到通知。使用-5值,我永远不会收到通知

我知道这里的好问题需要有一个可能的明确答案,这可能会受到很多“我也是”答案的制约--所以我要找的是苹果公司的官方(引用/链接),说这是预期行为,或者是上述代码的不同版本,按照文档所说的应该运行。

这对我的应用程序很重要,因为在某些情况下,即使警报发生在当天早些时候,我也需要通知用户。我想我可以修改我的代码来检查当前时间,并总是在超出该时间几秒钟后给出一个值——但我不确定“超出多少秒”是否真的安全,我希望它尽快发生——如果有更好的方法来获取“记录的行为”,我也不希望有这种攻击。我的真实代码与上面类似,但我发布了几个通知,有些可能是过去的,有些可能是今天晚些时候的,有些可能是明天以后的(这是一个日历应用程序)。

@eselk

我看到了与您相同的行为:如果通过在UIApplication对象上设置
scheduledLocalNotifications
属性来安装一个新创建的
UILocalNotification
,并且该通知过去具有fireDate,则不会激发

但是,如果使用UIApplication的
scheduleLocalNotification
方法安装了相同的
UILocalNotification
对象,它将立即触发

在我看来,这是一个基于的bug,它清楚地表明:

…当您设置[ScheduledLocalNotification]属性时,UILocalNotification 通过调用 cancelLocalNotification:然后调用scheduleLocalNotification: 对于每个新通知

考虑到情况并非如此,如果应用程序逻辑要求向用户显示过去调度的通知,则解决方法是调用scheduleLocalNotification

UILocalNotification *ln = [[UILocalNotification alloc]init];
[ln setFireDate:[NSDate dateWithTimeIntervalSinceNow:-2]]; // two seconds ago
// ...

// the following line works as expected - the notification fires immediately
[application scheduleLocalNotification:ln];  // Using this line works as expected

// using the following does NOT work as expected - the notification does not fire
//application.scheduledLocalNotifications = [NSArray arrayWithObject:ln];

(我在iOS 6模拟器上对此进行了测试)

谢谢您提供的信息。我最终只是自己处理通知,因为我只在我的应用程序处于前台时发布通知,所以如果他们“立即启动”,它只会调用我的应用程序。。。所以我把中间人移走了。我敢打赌,在用于ScheduledLocalNotification的iOS setter函数中,它只是跳过了已经发生的任何事件——在我看来,这仍然是一个bug(或者文档是错误的)。