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

问题描述 投票:0回答:1

在我的应用中,我正在通过以下方法安排本地通知:

    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设置>通知>应用名称>禁用“允许通知”
  2. 然后我打开应用并安排本地通知,例如2分钟后
  3. 此后,我返回到设置并启用“允许通知”开关。
  4. [2分钟后未显示本地通知。在较旧的iOS版本上对其进行了测试,并且通知如预期般显示。

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

ios swift localnotification unusernotificationcenter
1个回答
0
投票

Starsky是正确的!

根据Apple's Documentation

[Before尝试从您的应用程序安排本地通知,确保您的应用程序被授权这样做] >>,因为用户可以随时更改您的应用程序的授权设置。用户还可以更改应用程序允许的交互类型,这可能会导致您更改配置通知的方式。

如果禁用了权限,您将不得不“默默地失败”安排通知,或者通知用户他们需要进入“设置”应用以重新启用它们:

就我而言,我使用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)
    }
© www.soinside.com 2019 - 2024. All rights reserved.