簡體   English   中英

UNUserNotificationCenter 警報僅在前台解鎖/應用程序時觸發

[英]UNUserNotificationCenter alerts only firing when unlocked/app in foreground

我正在開發一個 iOS/Apple Watch 應用程序,其中包括一個計時器。 設置計時器后,它會在計時器完成時設置本地警報以觸發。 當應用程序打開並在前台運行時效果很好,但在鎖定屏幕或切換應用程序時不會發出警報。

相同的行為 iOS 和 Watch 該應用正在請求.alert.sound.badge權限,並且都被授予。 我試過在x秒內與在指定時間開火,同樣的行為我看到有人說它需要運行 60 秒以上,但這並沒有改變結果。

相關代碼:

            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { success, error in
                if  success {
                    print("Notification Permission Set")
                } else if let error = error {
                    print(error.localizedDescription)
                }
            }
            
            let content = UNMutableNotificationContent()
            content.title = "LE Timer"
            content.subtitle = "Timer Complete"
            content.sound = UNNotificationSound.default
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: runFor, repeats: false)
            notificationId = UUID().uuidString
            let request = UNNotificationRequest(identifier: notificationId, content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request)

發現問題:我有一個 function 應該在計時器停止時刪除通知,並且設置錯誤,所以它在計時器運行時刪除了通知。 我最后還設置了一個硬編碼的通知 ID,它可以工作,因為不應該同時有 2 個警報。

暫無
暫無

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

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