iOS Dev
iOS Dev

Reputation: 4248

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

In my app I am scheduling local notifications with the following method:

    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])
    }

Notifications are supposed to fire at a given time and should repeat every day at the same time.

On iOS 13 I found a bug that can be reproduced by the following steps:

  1. I go to iOS Settings > Notifications > App Name > Disable "Allow Notifications"
  2. Then I open the app and schedule a local notification for example after 2 minutes
  3. After that I go back to Settings and enable back "Allow Notifications" switch.
  4. After 2 minutes no local notification is shown. Tested it on older iOS versions and notifications are shown as supposed.

Maybe some people found this bug too and have any advice how to fix. Any help is appreciated.

Upvotes: 1

Views: 2820

Answers (1)

Graystripe
Graystripe

Reputation: 391

Starsky is correct!

According to 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.

You'll either have to "silently fail" at scheduling the notification if permissions are disabled, or notify the user that they need to go into the Settings app to re-enable them:

In my case, I did something like this using 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)
    }

Upvotes: 1

Related Questions