jpgpineda
jpgpineda

Reputation: 53

Getting Couldn’t communicate with a helper application. When trying to save a LocalNotification

Im gettin Couldn't comunicate with a helper application in the console when I'm trying to save a local notification in UNUserNotificationCenter with userInfo.

If I remove de userInfo instruccion works perfectly.

This is the class that implement NSCoding methods. In de notification.userInfo I'm storing an object of this class

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"
}

This is the code that add the notification and when I get the error:

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)")
            }
        }

This is the notification class:

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
}

Does anyone one know why this happened?

Upvotes: 3

Views: 232

Answers (1)

daveofthedead
daveofthedead

Reputation: 1

I saw this error when I had the userInfo dictionary containing a value other than a simple type. In my case it was a URL object, replacing it with a String resolved the error on adding the notification request.

Upvotes: 0

Related Questions