我在实施Apple的基本示例代码时遇到了麻烦,如下所示
这是我的代码;这是非常“教科书的例子”。我目前收到错误“'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)
答案 0 :(得分:3)
https://developer.apple.com/documentation/usernotifications/unnotificationsoundname
let soundName = UNNotificationSoundName("test.aiff")
content.sound = UNNotificationSound(named: soundName)