Nick Potts
Nick Potts

Reputation: 23

Declaring the title, subtitle, and body of a local notification

func scheduleNotification(inSeconds: TimeInterval, completion: @escaping (_ Success: Bool) -> ()) {

        let notif = UNNotificationContent()

         notif.title = NSString.localizedUserNotificationString(forKey: "New Notification", arguments: nil)
        notif.subtitle = "These are great!"
        notif.body = "The new notification options are awesome!"

        let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)

        let request = UNNotificationRequest(identifier: "myNotification", content: notif, trigger: notifTrigger)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
            if error != nil {
                print(error)
                completion(false)
            } else {
                completion(true)
            }

        })
    }

I am following along with a Udemy video and I am having a problem setting the title, subtitle, and body of a local notification. I get the same error for all three assignment lines.

Cannot assign to property: 'xxx' is a get-only property.

Upvotes: 1

Views: 1738

Answers (1)

SwiftedMind
SwiftedMind

Reputation: 4287

I quickly looked it up in the documentation. It says:

Do not create instances of this class directly. (...) For local notifications, create a UNMutableNotificationContent object and configure the contents of that object instead.

Source: https://developer.apple.com/documentation/usernotifications/unnotificationcontent

I'm not really familiar with this class but I think UNNotificationContent fills it content automatically from received data.

I'm not entirely sure if this is what you're looking for, but maybe try using UNMutableNotificationContent instead of UNNotificationContent:

let notif = UNMutableNotificationContent()

Upvotes: 1

Related Questions