我有我的本地通知的一個問題是:如果設備處於關閉狀態或改變手機的日期時,該通知隊列保持和隨着下一次通知一起開火。爲什麼?保證不對交貨UILocalNotification在正確的時間IOS
因爲需要非常關注通知日期.. 如何確保通知的正確傳遞?
我們應該在哪裏放置刪除過期通知的代碼?
我有我的本地通知的一個問題是:如果設備處於關閉狀態或改變手機的日期時,該通知隊列保持和隨着下一次通知一起開火。爲什麼?保證不對交貨UILocalNotification在正確的時間IOS
因爲需要非常關注通知日期.. 如何確保通知的正確傳遞?
我們應該在哪裏放置刪除過期通知的代碼?
所以你的意思是你有一個名爲ABC的通知在30分鐘內被觸發......但是你關掉iPhone 3小時......然後打開它,看到通知ABC通知,即使它的時間是通過呢? AFAIK ....通知仍然存在除非您使用removeDeliveredNotifications(withIdentifiers:)
刪除它。
基本上撥打:
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).
欲瞭解更多信息,請參閱本moment這個WWDC視頻。
也看到這個tutorial認準節「4.管理的通知」
作爲解決您的編輯:
說實話,我不知道......但這裏是我想你應該做的:
我建議,使創建通知的類:
第一步:順應UNUserNotificationCenterDelegate
第二步:實現:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping() -> Void) {
}
現在,你把你的設備上...此功能將被調用只有如果應用程序是在後臺。在那裏你有機會殺掉你過時的通知。
這個函數在前臺和後臺都調用,如果是後臺,那麼你可以在步驟3後殺掉過時的通知,如果前臺的好,那麼你不需要做任何事情,它會自己發送隊列,因爲它被顯示!
第三步:
裏面你實現的功能做:
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
。 (雖然我不確定你的計時器是否會重置自己,但我認爲它不應該,而且你很好)。
可能重複的[關閉已交付的UILocalNotification?](https://stackoverflow.com/questions/10652274/dismiss-an-already-delivered-uilocalnotification) – Honey
或可能重複[this](https:// stackoverflow.com/questions/38922892/how-can-i-removed-previously-delivered-notifications-when-a-new-notification-arr)。如果你使用的是iOS 10,那麼我的答案中的鏈接將有足夠的信息... – Honey
在你的標題中,你的意思是_correct_或_current_? –