iOS 11 用户自定义声音通知

Ken*_*ong 4 ios swift watchkit

我在实现 Apple 的基本示例代码时遇到了麻烦,如下所示

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html#//apple_ref/doc/uid/TP40008194-CH5-SW3

这是我的代码;这是非常“教科书上的例子”。我当前收到错误“'init(named:)' 不可用”

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey:"testreminder!", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey:"Reminder body.", arguments: nil)

    content.sound = UNNotificationSound(named: "test.aiff");
    content.categoryIdentifier = "myCategory"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "test.aiff", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

编辑:在尝试建议后,这是代码,但它不起作用。它给出“init(named:)”不可用

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey:"testreminder!", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey:"Reminder body.", arguments: nil)

    content.sound = UNNotificationSound.init(named: "test.aiff")
    content.categoryIdentifier = "myCategory"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: "test.aiff", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

luk*_*asa 6

  1. 使用 UNNotificationSoundName 结构来定义声音名称。

https://developer.apple.com/documentation/usernotifications/unnotificationsoundname

let soundName = UNNotificationSoundName("test.aiff")
content.sound = UNNotificationSound(named: soundName)
Run Code Online (Sandbox Code Playgroud)
  1. 并确保在文件检查器中检查声音文件的目标成员资格。文件检查器截图