簡體   English   中英

如果在 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 設置 > 通知 > 應用程序名稱 > 禁用“允許通知”
  2. 然后我打開應用程序並安排本地通知,例如在 2 分鍾后
  3. 之后,我返回設置並啟用“允許通知”開關。
  4. 2 分鍾后沒有顯示本地通知。 在較舊的 iOS 版本上對其進行了測試,並按預期顯示通知。

也許有些人也發現了這個錯誤,並對如何修復有任何建議。 任何幫助表示贊賞。

星空是對的!

根據蘋果的文檔

嘗試從您的應用程序安排本地通知之前,請確保您的應用程序已獲得授權,因為用戶可以隨時更改您的應用程序的授權設置。 用戶還可以更改您的應用允許的交互類型,這可能會導致您更改配置通知的方式。

如果權限被禁用,您將不得不在安排通知時“靜默失敗”,或者通知用戶他們需要進入“設置”應用程序以重新啟用它們:

就我而言,我使用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)
    }

暫無
暫無

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

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