gpt4 book ai didi

ios - 用户信息中带有自定义对象的 UNMutableNotificationContent

转载 作者:IT王子 更新时间:2023-10-29 05:36:50 24 4
gpt4 key购买 nike

我想在 UNMutableNotificationContent 的用户信息中使用自定义对象,但它不起作用。当我将自定义对象放入 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!)
}
}

对于以下内容,没有错误但不会触发通知:

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: "alarmNotification", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
UNUserNotificationCenter.current().delegate = self
if error != nil {
print(error!)
}
}

TestClass是自定义类,这里是定义:

class TestClass: NSObject, NSSecureCoding {
public var progress: Float = 0

required override public init() {
super.init()
}

public init(progress: Float) {
self.progress = progress
}

public required convenience init?(coder aDecoder: NSCoder) {
self.init()
progress = aDecoder.decodeObject(forKey: "progress") as! Float
}

public func encode(with aCoder: NSCoder) {
aCoder.encode(progress, forKey: "progress")
}

public static var supportsSecureCoding: Bool {
get {
return true
}
}

}

有什么想法吗?

最佳答案

你的object应该是Property List

The keys in this dictionary must be property-list types—that is, they must be types that can be serialized into the property-list format. For information about property-list types, see Property List Programming Guide.

您可以将您的对象转换为 NSData(使用 NSArchiver 将其存档)

关于ios - 用户信息中带有自定义对象的 UNMutableNotificationContent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com