所以你的意思是你有一个名为 ABC 的通知要在 30 分钟内触发......但是你关闭你的 iPhone 3 小时......然后打开它并看到通知 ABC 通知,即使它的时间已经过去了? AFAIK ....通知将仍然存在除非您使用 removeDeliveredNotifications(withIdentifiers:) 将其删除。
基本调用:
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).
有关更多信息,请参阅此 WWDC 视频的 moment。
另见tutorial 查找“4.管理通知”部分
关于您的编辑:
说实话,我不确定……但我认为你应该这样做:
我建议创建创建通知的类:
第一步:符合UNUserNotificationCenterDelegate
第二步:实施:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
}
现在您已打开设备...仅在应用处于后台时才会调用此函数。在那里你有机会杀死你过时的通知。
这个函数在前台和后台都被调用,如果是后台,那么你可以在step3之后杀死过时的通知,如果前台很好,那么你不需要做任何事情,它会自己发送队列,因为它被显示!
第三步:
在你实现的功能做了:
UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in
print("\(deliveredNotifications.count) Delivered notifications-------")
for notification in deliveredNotifications{
if (someCondition){
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).
}
}
})
至于 iPhone 时间更改问题...您应该提出一个合理的条件并找到通知并将其删除...或者只需将您的通知触发器从 UNCalendarNotificationTrigger 更改为 UNTimeIntervalNotificationTrigger。 (虽然我再次不确定你的计时器是否会自行重置。我认为它不应该而且你很好)。