如果在 iOS 设置中禁用通知访问时安排本地通知,则不会在 iOS 13 上触发

Local notification not fired on iOS 13 if it was scheduled when notifications access is disabled from iOS Settings

在我的应用程序中,我使用以下方法安排本地通知:

    func addNotificationRequest(fireDate: Date, identifier: String, sound: UNNotificationSound)
    {
        let notificationCenter = UNUserNotificationCenter.current()
        let content = UNMutableNotificationContent()
        content.title = "Important"
        content.body = notificationMessage
        content.sound = sound
        content.categoryIdentifier = "UserActions"

        let calendar = Calendar(identifier: .gregorian)
        let triggerDate = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)

        let notificationRequest = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
        notificationCenter.add(notificationRequest) { error in

            if let error = error
            {
                print(error.localizedDescription)
            }
        }

        let myAction = UNNotificationAction(identifier: "MyActionID", title: "Open", options: [.foreground])
        let category = UNNotificationCategory(identifier: "UserActions", actions: [myAction], intentIdentifiers: [], options: [])
        notificationCenter.setNotificationCategories([category])
    }

通知应该在给定的时间触发,并且应该在每天的同一时间重复。

在 iOS 13 我发现了一个可以通过以下步骤重现的错误:

  1. 我转到 iOS 设置 > 通知 > 应用程序名称 > 禁用 "Allow Notifications"
  2. 然后我打开应用程序并安排本地通知,例如 2 分钟后
  3. 之后我返回“设置”并启用返回 "Allow Notifications" 开关。
  4. 2 分钟后没有显示本地通知。在较旧的 iOS 版本上对其进行了测试,并且通知按预期显示。

也许有些人也发现了这个错误,并且对如何修复有任何建议。任何帮助表示赞赏。

星空是正确的!

根据Apple's Documentation

Before trying to schedule local notifications from your app, make sure that your app is authorized to do so, because the user can change your app’s authorization settings at any time. Users can also change the types of interactions that are allowed for your app, which might cause you to change the way you configure your notifications.

如果权限被禁用,您将不得不 "silently fail" 安排通知,或者通知用户他们需要进入“设置”应用以重新启用它们:

就我而言,我使用 SwiftMessages:

做了类似的事情
static func showNotificationDisabledInfo() {
        print("INFO: Notification permissions denied, need to reset in Settings!")
        showAlertMessage(withTitle: "Notifications are disabled!",
                         body: "Go to the Settings app to re-enable notifications.",
                         type: .info)
    }