首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift UNUserNotification不会触发声音

Swift UNUserNotification不会触发声音
EN

Stack Overflow用户
提问于 2017-02-18 17:15:51
回答 1查看 3.4K关注 0票数 3

这是我的UNUserNotification代码

代码语言:javascript
复制
     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)
        }
    }

它在调用后10秒安排通知,它工作,通知显示在锁定屏幕上,问题是它不触发任何声音/振动。

如果您要求我设置这些选项,[.alert, .sound, .badge],我遗漏了什么?

附注:我是不愿意设置一个自定义声音,默认的声音就足够了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-18 17:30:35

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

代码语言:javascript
复制
notificationContent.sound = UNNotificationSound.default() 

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42318209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档