swift - 无法与帮助应用程序通信。尝试保存 LocalNotification 时
问题描述
当我尝试使用 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, description: String, homeWork: Homework) {
self.id = id
self.title = title
self.subTitle = subtitle
self.dateTime = dateTime
self.type = type
self.body = description
self.homework = homeWork
}
有谁知道为什么会这样?
解决方案
推荐阅读
- r - R以不同颜色打印数据点组
- python - 创建生成器以相反的顺序读取文本文件中的每一行
- sql - 当添加/更新发生在另一个表中时,如何通过 SQL Server 中的存储过程在表中添加/更新记录
- eclipse - 忽略 .gitignore 中的所有子文件夹/文件
- javascript -
- 得到一些元素后消失
- html - 将样式应用于标签时,使样式覆盖另一种样式
- excel - 向文件夹中的所有工作簿添加新的和删除旧的 VBA
- git - “git grep
" 仅适用于 python(或任何类型的)文件? - html - CSS ul li 格式化
- python - 激活 python 虚拟环境后,输入 `python` 仍然会得到 PATH 中的版本