【问题标题】:UNUserNotificationCenter alerts only firing when unlocked/app in foregroundUNUserNotificationCenter 警报仅在前台解锁/应用程序时触发
【发布时间】:2021-02-18 06:01:12
【问题描述】:

我正在开发一个包含计时器的 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)

【问题讨论】:

    标签: ios swift apple-watch notificationcenter


    【解决方案1】:

    发现问题:我有一个函数应该在计时器停止时删除通知,但它设置错误,所以它在计时器运行时删除了通知。我最后还设置了一个硬编码的通知 ID,因为不应该同时有 2 个警报。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 2020-10-28
      相关资源
      最近更新 更多