简体   繁体   English

iOS中计划的本地通知和时区更改

[英]Scheduled local notification and timezone changes in iOS

I have this method in my app to fire scheduled local notifications: 我的应用程序中有此方法可以触发计划的本地通知:

- (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];
}

It works fine if user does not change her timezone in her device's Settings, but the notification is not launched if she changes the timezone after it has been already scheduled but not launched yet. 如果用户未在设备的“设置”中更改时区,则该方法工作正常,但是如果在已安排好时区之后仍未启动时区,则通知不会启动。 I've also tried with [NSTimeZone defaultTimeZone] with no success. 我也尝试了[NSTimeZone defaultTimeZone] ,但没有成功。 Is there any way to detect these timezone changes and refresh the fireDate for already scheduled local notifications? 有什么方法可以检测到这些时区变化并为已调度的本地通知刷新fireDate

Thanks 谢谢

You can do it in 2 ways 您可以通过2种方式完成

  1. implement the following method in your appdelegate 在您的appdelegate中实现以下方法
  • (void)applicationSignificantTimeChange:(UIApplication *)application (void)applicationSignificantTimeChange:(UIApplication *)应用程序

2 . 2。 Register for UIApplicationSignificantTimeChangeNotification in notificationcenter 在Notificationcenter中注册UIApplicationSignificantTimeChangeNotification

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM