【问题标题】:Scheduled local notification and timezone changes in iOSiOS 中计划的本地通知和时区更改
【发布时间】:2014-08-27 16:58:30
【问题描述】:

我的应用中有这个方法可以触发预定的本地通知:

- (void)scheduleNotificationForTime:(long long)atTime
{
   NSDate *fireDate = [DatesMngr getDateFromLongLong:atTime];

   UILocalNotification *localNotif = [[UILocalNotification alloc] init];
   if (localNotif == nil)
       return;
   localNotif.fireDate = fireDate;
   localNotif.timeZone = [NSTimeZone localTimeZone];

   localNotif.alertBody = @"Active Status";
   localNotif.alertAction = @"View Details";
   localNotif.applicationIconBadgeNumber = 1;

   [[UIApplication sharedApplication] cancelAllLocalNotifications];
   [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

如果用户没有在她的设备设置中更改她的时区,它工作正常,但如果她在已经安排但尚未启动后更改时区,则不会启动通知。我也尝试过[NSTimeZone defaultTimeZone],但没有成功。有什么方法可以检测到这些时区更改并刷新fireDate 以获取已安排的本地通知?

谢谢

【问题讨论】:

    标签: ios timezone scheduled-tasks uilocalnotification


    【解决方案1】:

    你可以通过两种方式做到这一点

    1. 在您的 appdelegate 中实现以下方法
    • (void)applicationSignificantTimeChange:(UIApplication *)application

    2 .在通知中心注册UIApplicationSignificantTimeChangeNotification

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTimeChange:)];

    【讨论】:

    • 谢谢,但这只有在更改设备设置中的时区后,应用程序再次激活并且我收到已安排的本地通知并重新设置其timeZone 时才有效,对?如果用户只是更改了时区,并且在我希望启动通知的时间之前没有再次打开应用程序怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多