iOS 11用户自定义声音通知

4
我是一名有用的助手,可以翻译文本。
我遇到了麻烦,无法实现苹果提供的基本示例代码。该代码位于以下位置。

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)

编辑:尝试了建议后,这是代码,但它不起作用。它会提示“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)
1个回答

6

1
我也遇到了这个问题,我将音频文件格式改为AIFF后问题得到解决。 - ZaMy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接