簡體   English   中英

已安排iOS UILocalNotification,但未觸發

[英]iOS UILocalNotification scheduled, but not fired

我正在制作需要本地通知的應用程序。 而且正在發生奇怪的事情。 我安排了通知:

static func setNotification(body: String, departure: Double, notification_info: Dictionary<String, String> )
{
    let notification = UILocalNotification()
    notification.alertBody = body
    notification.fireDate =  NSDate(timeIntervalSinceNow: departure) 
    notification.alertAction = "ShowDetails"
    notification.userInfo = notification_info
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    print("notification over: " + String(departure))
    print("-------" + String( notification.fireDate ))
}

我要打印多少秒鍾才能收到通知。 我進入后台模式,一直在等待何時收到通知。 時間過去后,即使我確定自己處於后台模式,也不會收到任何通知。 (在Xcode中,我查看調試導航器> Energy Impact,它表示我處於后台)。

當我重新啟動手機並運行該應用程序時,它會顯示通知。 一切正常。 然后,在經過更多測試並使用了該應用程序之后,我的通知再次停止工作(即使通知仍在計划中。我會使用以下所有計划的通知:

        UIApplication.sharedApplication().scheduledLocalNotifications

我對Swift還是很陌生,我也不知道為什么會這樣。 這讓我發瘋,即使通知已安排好一切,也不知道為什么我的通知沒有被觸發...

有人可以幫我嗎? 如果您需要更多信息,請詢問。

  • 編輯:
    • 我確實設置了權限
    • 通知在AppDelegate中設置
    • 出發時間絕對正確

我沒有在通知中添加唯一的ID(notification.userInfo並非始終是唯一的)而犯了一個錯誤,並覆蓋了之前的預定通知。 這就是為什么某些東西行之有效,有時卻行不通的原因。

通過使其唯一來解決該問題。 謝謝

從iOS 8.0開始,您必須請求將本地通知作為遠程通知的權限:

let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)   

位置通知中查看更多詳細信息

暫無
暫無

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

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