簡體   English   中英

如何在iOS中僅發布一次UILocalNotifivcation

[英]How to Post a UILocalNotifivcation only once in iOS

我在iOS中發布通知,我不想重復使用的通知代碼。

let notification = UILocalNotification()
notification.timeZone = NSTimeZone.local
//calculation of dateTime
notification.fireDate = dateTime as Date?


notification.repeatInterval = 0

notification.alertBody = "Body"
notification.userInfo = ["title": "Notify", "type": "title", "5436" : "Notify"]

在這種情況下,我的通知沒有觸發。 當我給像

notification.repeatInterval = NSCalendar.Unit.day

正在射擊。這是功能還是問題? 我如何在沒有重復間隔的情況下發布通知

解決方案1:您可以使用dispatch_once

static var token: dispatch_once_t = 0

dispatch_once(&token) {
  NSLog("Do it once")
}

解決方案2:

取自Apple文檔:

如果您分配日歷單位,例如每周(NSWeekCalendarUnit)或每年(NSYearCalendarUnit),則系統將按指定的時間間隔重新安排通知的發送時間。 默認值為0,表示不重復。

我假設您想以重復間隔取消通知,您可以通過兩種方式做到這一點:

取消所有通知。

僅取消該通知。

第一種選擇很簡單,使用:

cancelAllLocalNotifications在您的應用程序委托中。

第二個需要更多的工作。 您需要瀏覽待處理的通知(使用ScheduledLocalNotifications)並取消通知。

為了知道要取消的通知,可以在設置通知時使用userInfo屬性。 例如,為每個通知設置唯一的ID,這樣當您取消該通知時,只需將該ID與ScheduledLocalNotifications數組中的所有ID進行比較即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM