gpt4 book ai didi

ios - Swift UNUserNotification 不触发声音

转载 作者:可可西里 更新时间:2023-11-01 00:35:50 29 4
gpt4 key购买 nike

这是我的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)
}
}

它在调用后 10 秒安排通知并且有效,通知显示在锁定屏幕上,问题是它不会触发任何声音/振动。
根据要求,我正在设置这些选项 [.alert, .sound, .badge],我错过了什么?
附:我不愿意设置自定义声音,默认一个就够了

最佳答案

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

notificationContent.sound = UNNotificationSound.default() 

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

关于ios - Swift UNUserNotification 不触发声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318209/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com