【问题标题】:Local Notification not triggering after changing device time in iOS在 iOS 中更改设备时间后未触发本地通知
【发布时间】:2020-12-08 12:45:08
【问题描述】:

我在我的一个应用程序中设置了本地通知,它会定期提醒用户有关药物的信息,每天 n 次取决于用户的选择。如果用户在应用程序中设置提醒并更改设备设置的日期,则提醒不会在下一个预定时间触发。但在其他预定时间工作正常。缺少第一个预定通知。在正常情况下工作正常。有人遇到过类似的问题吗? TIA。代码sn-p如下:

 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;

[center setNotificationCategories:[NSSet set]];
    
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge;
    [center requestAuthorizationWithOptions:options
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!granted) {
                                  PRLog(@"Something went wrong");
                              }else{
                                  dispatch_async(dispatch_get_main_queue(), ^{
                                      [[UIApplication sharedApplication] registerForRemoteNotifications];
                                  });
                                  
                                  [LocalNotificationShared registerCategories];
                              }
                          }];

}

触发部分如下:

NSDateComponents *triggerDate = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:date]; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:NO];

NSString *s = [NSString stringWithFormat:@"%@",@([NSDate timeIntervalSinceReferenceDate])];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:s
                                                                         content:content trigger:trigger];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
       if (error != nil) {
           PRLog(@"Something went wrong: %@",error);
       }
    }];

【问题讨论】:

    标签: ios objective-c notifications uilocalnotification


    【解决方案1】:

    据我观察,计划通知的时间间隔与设备时间无关。因此,如果您将通知安排在一个小时后,然后将您的设备时间设置为提前 59 分钟,那也没关系 - 您仍然需要等待整整一个小时才能触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2017-02-28
      相关资源
      最近更新 更多