2017-10-07 37 views
0
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) 
      } 

     }) 
    } 

我跟隨Udemy視頻一起關注,並且遇到了設置本地通知的標題,副標題和正文的問題。我得到了所有三個賦值行的相同錯誤。聲明本地通知的標題,副標題和主體

不能分配給屬性:'xxx'是一個只讀屬性。

回答

0

我很快在文檔中查了一下。它說:

不要直接創建此類的實例。 (...)對於本地通知,請創建一個UNMutableNotificationContent對象,然後配置該對象的內容。

來源:https://developer.apple.com/documentation/usernotifications/unnotificationcontent

我不是很熟悉這個類,但我認爲UNNotificationContent填充它從接收到的數據自動的內容。

我不能完全肯定,如果這是你在找什麼,但也許嘗試使用UNMutableNotificationContent代替UNNotificationContent

let notif = UNMutableNotificationContent()