Это мой код для UNUserNotification
Swift UNUserNotification не вызывает звук
func scheduleNotification() {
UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
switch notificationSettings.authorizationStatus {
case .notDetermined:
self.requestAuthorization(completionHandler: { (success) in
guard success else { return }
// Schedule Local Notification
self.scheduleLocalNotification()
})
case .authorized:
// Schedule Local Notification
self.scheduleLocalNotification()
case .denied:
print("Application Not Allowed to Display Notifications")
}
}
}
private func scheduleLocalNotification() {
// Create Notification Content
let notificationContent = UNMutableNotificationContent()
// Configure Notification Content
notificationContent.title = "Hello"
notificationContent.body = ""
// Add Trigger
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)
// Create Notification Request
let notificationRequest = UNNotificationRequest(identifier: id, content: notificationContent, trigger: notificationTrigger)
// Add Request to User Notification Center
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error {
print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
}
}
}
private func requestAuthorization(completionHandler: @escaping (_ success: Bool) ->()) {
// Request Authorization
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in
if let error = error {
print("Request Authorization Failed (\(error), \(error.localizedDescription))")
}
completionHandler(success)
}
}
It расписаний уведомления через 10 секунд после вызова, и она работает, уведомление представляется на экране блокировки, проблема заключается в том, что она делает не вызывать звук/вибрацию.
По запросу Я устанавливаю эти опции [.alert, .sound, .badge]
, что мне не хватает?
p.s. Я не готовы установить пользовательский звук, по умолчанию один достаточно
Да, это недостающая линия, спасибо. Хотя мне кажется немного странным, что я должен явно установить значение по умолчанию, учитывая тот факт, что я запросил «.sound» в настройках :) – r4id4
Добро пожаловать! Я бы согласился; способ уведомления вообще работает немного kludgy - удаленные уведомления очень сильно. –