uilocalnotification 没有在准确的时间触发

发布于 2024-10-18 12:11:50 字数 158 浏览 1 评论 0原文

我想在我的应用程序中实现uilocalnotification。但问题是它没有在准确的时间发射。它在给定发射时间后 30 - 40 秒后发射。 是否有我遗漏的东西,或者这是 UILocalNotification 中的常见想法。

谢谢

I want to implement uilocalnotification in my app. but the problem is that it is not firing at the exact time. it is firing after 30 - 40 secs from the given firing time.
Is there something I am missing or this is a common think in UILocalNotification.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷心人i 2024-10-25 12:11:50

UILocalNotification 是为遥远的未来发生的事情而设计的,因此 30 秒的错误是可以接受的(即,如果我在 15:00 开会,那么在 15:00:30 告诉我就不成问题了:)

如果您想要比保持应用程序运行和使用 NSTimer 所需的更高的准确性。

UILocalNotification is designed for things that are happening far in the future so an error of 30 seconds would be OK (i.e. if I had a meeting at 15:00 then telling me at 15:00:30 wouldn't be a problem :)

If you want more accuracy than that you will need to keep your app running and use an NSTimer.

弥繁 2024-10-25 12:11:50

我知道这是一个老问题,但我刚刚弄清楚,你可以使用
dateByAddingTimeInterval: 方法。

notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30];

I know this is an old question, but I have just figure this out, you can use the
dateByAddingTimeInterval : method.

notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30];
请远离我 2024-10-25 12:11:50

您可以使用NSDateComponents来设置fireDate,在其中您可以设置firedate的秒数。当我将其设置为 0 时不起作用,但如果将其设置为 1 秒则有效。与延迟 30-40 秒相比,延迟 1 秒就可以了。

NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dateComps = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond   fromDate:fireDate];
    [dateComps setSecond:1];
    [dateComps setNanosecond:00];
    NSDate *newDate = [cal dateFromComponents:dateComps];
    fireDate = newDate;

You can set fireDate by using NSDateComponents, in which you can set seconds for firedate. When I set it to 0 doesn't work, but it works if I set it to 1 seconds. 1 second late will be OK compared to 30-40 seconds.

NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dateComps = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond   fromDate:fireDate];
    [dateComps setSecond:1];
    [dateComps setNanosecond:00];
    NSDate *newDate = [cal dateFromComponents:dateComps];
    fireDate = newDate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文