保证在正确的时间ios交付的UILocalNotification
我的本地通知存在问题: 如果设备处于closures状态或更改电话date ,通知将保持在队列中,并随着下一个通知一起开始。 为什么?
由于需要非常关注通知date。如何保证正确的通知发送?
我们应该在哪里放置删除过期通知的代码?
所以你的意思是你有一个名为ABC的通知在30分钟内被触发…但是你关掉了你的iPhone 3个小时……然后打开它,看到通知ABC通知,即使它的时间通过了? AFAIK ….通知将仍然存在, 除非您使用removeDeliveredNotifications(withIdentifiers:)
删除它。
基本上来说:
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).
欲了解更多信息,请参阅此WWDCvideo的这一刻 。
另请参阅本教程,查找“4.pipe理通知”
至于解决您的编辑:
说实话,我不确定…但这是我认为你应该做的:
我build议创build通知的类:
第一步:符合UNUserNotificationCenterDelegate
第二步:实施:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { }
现在你打开了你的设备… 只有当应用程序在后台才能调用该函数。 在那里你有机会杀死你过时的通知。
这个函数在前台和后台都调用,如果是后台的话你可以在第三步之后杀掉过时的通知,如果前台的好,那么你不需要做任何事情,它会自动发送队列,因为它被显示了!
第三步:
在你实现的function里面 :
UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in print("\(deliveredNotifications.count) Delivered notifications-------") for notification in deliveredNotifications{ if (someCondition){ UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]). } } })
至于iPhone的时间变化问题…你应该拿出一个合理的条件,并find通知,并删除它…或者只是将通知触发器从UNCalendarNotificationTrigger
为UNTimeIntervalNotificationTrigger
。 (虽然我不确定你的计时器是否会自动复位,但我认为它不应该,而且你很好)。