ios - 在 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);
}
}];
解决方案
据我观察,预定通知的时间间隔与设备时间无关。因此,如果您安排一个小时后的通知,然后将您的设备时间设置为提前 59 分钟,那没关系 - 您仍然需要等待整整一个小时才能触发它。
推荐阅读
- javascript - 如何多次调用javascript
- javascript - Javascript 转换实现,如 css
- flutter - 在颤动中左右移动对象
- javascript - 选择检查主体时如何更新 MongoDB 中的数据
- rest - 一般来说,为用户设置两条 GET 路由(一条用于 ID,一条用于用户名)是否是多余的?
- c# - 在随机行和列中排列对象
- python - 从运行 python 脚本导入变量
- reactjs - 打字稿中的调度类型
- azure-devops - 我有 yaml 管道,它将在 4 个区域中使用 tf 创建我的 az 资源(rg、vnet、aks ..)。使用相同的 piplin 我想在 2 个区域中创建 acr?
- css - 我有一个 CSS,我希望它内联对齐