微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法与助手应用程序通信尝试保存LocalNotification时

如何解决无法与助手应用程序通信尝试保存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,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 举报,一经查实,本站将立刻删除。