我想在UNMutableNotificationContent的userinfo中使用cutom对象,但它不起作用.当我在userinfo中放置自定义对象时,不会触发通知.
使用此代码,会触发通知:
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.categoryIdentifier = "alarmNotificationCategory"
content.sound = UNNotificationSound.default()
content.userInfo = ["myKey": "myValue"] as [String : Any]
let request = UNNotificationRequest(identifier: "alarmNotification", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
UNUserNotificationCenter.current().delegate = self
if error != nil {
print(error!)
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下内容,不会发出错误,但不会触发通知:
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.categoryIdentifier = "alarmNotificationCategory"
content.sound = UNNotificationSound.default()
content.userInfo = ["myKey": TestClass(progress: 2)] as [String : Any]
let request = UNNotificationRequest(identifier: …Run Code Online (Sandbox Code Playgroud)