iOS 11用户自定义声音通知

时间:2017-12-01 04:42:46

标签: ios swift watchkit

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

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

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

    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)

编辑:在尝试建议时,这是代码,但它无法正常工作。它给出'init(命名:)'不可用

    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)

1 个答案:

答案 0 :(得分:3)

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

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

let soundName = UNNotificationSoundName("test.aiff")
content.sound = UNNotificationSound(named: soundName)
  1. 并确保在文件检查器中检查目标成员身份中的声音文件。File inspector Screenshot