问题描述
当我试图使用userInfo在UNUserNotificationCenter中保存本地通知时,无法与控制台中的助手应用程序通信。
如果我删除了de userInfo,则说明Instruccion可以正常工作。
这是实现NSCoding方法的类。在de notification.userInfo中,我存储了此类的对象
class Homework: NSObject,NSCoding,NSSecureCoding {
let name: String
let homeworkDescription: String
let date: Date
let score: Double
let status: String
let subject: String
let db = Firestore.firestore()
static var supportsSecureCoding: Bool {
return true
}
init(name: String,description: String,date: Date,score: Double,status: String,subject: String){
self.name = name
self.homeworkDescription = description
self.date = date
self.score = score
self.status = status
self.subject = subject
}
func encode(with coder: NSCoder) {
coder.encode(name,forKey: Keys.name.rawValue)
coder.encode(homeworkDescription,forKey: Keys.description.rawValue)
coder.encode(date,forKey: Keys.date.rawValue)
coder.encode(score,forKey: Keys.score.rawValue)
coder.encode(status,forKey: Keys.status.rawValue)
coder.encode(subject,forKey: Keys.subject.rawValue)
}
required convenience init?(coder: NSCoder) {
let decodedName = coder.decodeObject(forKey: Keys.name.rawValue) as! String
let decodedDescription = coder.decodeObject(forKey: Keys.description.rawValue) as! String
let decodedDate = coder.decodeObject(forKey: Keys.date.rawValue) as! Date
let decodedScore = coder.decodeDouble(forKey: Keys.score.rawValue)
let decodedStatus = coder.decodeObject(forKey: Keys.status.rawValue) as! String
let decodedSubject = coder.decodeObject(forKey: Keys.subject.rawValue) as! String
self.init(name: decodedName,description: decodedDescription,date: decodedDate,score: decodedScore,status: decodedStatus,subject: decodedSubject)
}
enum Keys: String {
case name = "Name"
case description = "Description"
case date = "Date"
case score = "Score"
case status = "Status"
case subject = "Subject"
}
这是添加通知的代码,当我收到错误消息时:
for notification in notifications
{
let content = UNMutableNotificationContent()
content.title = notification.title
content.subtitle = notification.subTitle
content.body = notification.body
content.sound = .default
content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber
content.targetContentIdentifier = notification.type
content.userInfo[notification.type] = notification.homework
let trigger = UNCalendarNotificationTrigger(dateMatching: notification.dateTime,repeats: false)
let request = UNNotificationRequest(identifier: notification.id,content: content,trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error{
print("This is the error: " + error.localizedDescription)
}else{
print("Notification scheduled! --- ID = \(notification.id)")
}
}
这是通知类:
class LocalNotification {
var id: String
var title: String
var subTitle: String
var dateTime: DateComponents
var type: String
var body: String
var homework: Homework
init(id: String,title: String,subtitle: String,dateTime: DateComponents,type: String,homeWork: Homework) {
self.id = id
self.title = title
self.subTitle = subtitle
self.dateTime = dateTime
self.type = type
self.body = description
self.homework = homeWork
}
有人知道为什么会这样吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)