Swift UNUserNotification does not trigger sound
Asked Answered
E

1

5

This is my code for 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 schedules the notification 10 seconds after the call and it works, the notification is presented on the lock screen, the problem is that it does not trigger any sound/vibration.
Upon request I'm setting these options [.alert, .sound, .badge], what am I missing?
p.s. I am not willing to set a custom sound, the default one is enough

Elsi answered 18/2, 2017 at 17:15 Comment(0)
B
7

You are missing the critical element that will play the sound:

notificationContent.sound = UNNotificationSound.default() 

Including UNMutableNotificationContent should solve your issue.

Burmaburman answered 18/2, 2017 at 17:30 Comment(3)
Yes that's the missing line, thanks. Even though It looks a little bit odd to me that I have to explicitly set the default value given the fact I requested .sound in the options :)Elsi
You're welcome! I would agree; the way notifications work in general is a bit kludgy — remote notifications very much so.Haberdasher
Even after setting this default sound still, it doesn't work for me.Cryptomeria

© 2022 - 2024 — McMap. All rights reserved.