首页 > 解决方案 > 在 iOS 中更改设备时间后未触发本地通知

问题描述

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

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

标签: iosobjective-cnotificationsuilocalnotification

解决方案


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


推荐阅读