首页 > 解决方案 > 无法与帮助应用程序通信。尝试保存 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
}

有谁知道为什么会这样?

标签: swiftusernotifications

解决方案


推荐阅读