声明本地通知的标题,副标题和主体
问题描述:
func scheduleNotification(inSeconds: TimeInterval, completion: @escaping (_ Success: Bool) ->()) {
let notif = UNNotificationContent()
notif.title = NSString.localizedUserNotificationString(forKey: "New Notification", arguments: nil)
notif.subtitle = "These are great!"
notif.body = "The new notification options are awesome!"
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)
let request = UNNotificationRequest(identifier: "myNotification", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
if error != nil {
print(error)
completion(false)
} else {
completion(true)
}
})
}
我跟随Udemy视频一起关注,并且遇到了设置本地通知的标题,副标题和正文的问题。我得到了所有三个赋值行的相同错误。声明本地通知的标题,副标题和主体
不能分配给属性:'xxx'是一个只读属性。
答
我很快在文档中查了一下。它说:
不要直接创建此类的实例。 (...)对于本地通知,请创建一个UNMutableNotificationContent对象,然后配置该对象的内容。
来源:https://developer.apple.com/documentation/usernotifications/unnotificationcontent
我不是很熟悉这个类,但我认为UNNotificationContent
填充它从接收到的数据自动的内容。
我不能完全肯定,如果这是你在找什么,但也许尝试使用UNMutableNotificationContent
代替UNNotificationContent
:
let notif = UNMutableNotificationContent()