Reputation: 4248
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:
Maybe some people found this bug too and have any advice how to fix. Any help is appreciated.
Upvotes: 1
Views: 2820
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