Swift UNUserNotification不触发声音

r4i*_*id4 2 ios swift unnotificationrequest unnotificationtrigger

这是我的代码 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)
        }
    }
Run Code Online (Sandbox Code Playgroud)

它在呼叫后的10秒钟内安排了通知,并且通知生效,通知显示在锁定屏幕上,问题是它不会触发任何声音/振动。
根据要求,我将设置这些选项[.alert, .sound, .badge],我缺少什么?
PS我舍得设置自定义音效,预设一个就够了

l'L*_*L'l 5

您缺少播放声音的关键元素:

notificationContent.sound = UNNotificationSound.default() 
Run Code Online (Sandbox Code Playgroud)

包括UNMutableNotificationContent应该可以解决您的问题。