0

I am looking to create a local notification system where the text it displays is different than the text it contains at request. I'm not looking to modify the notification from the standard Title, Body format. To test this, I tried to create a notification showing the current date, but when it was displayed, the time shown was two minutes ago. Is it possible to have this notification show the current date and time (or other variable content) without push notifications?

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy MM dd HH mm ss"
let dateString = dateFormatter.string(from: date)
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "the date is \(date)"
content.sound = UNNotificationSound.default

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: false)

let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Luke Redmore
  • 449
  • 3
  • 10
  • I'm not looking to change how the content is displayed in the preview, just wondering if there is a way to display a notification of the current date and time string, instead of the time that the notification was requested – Luke Redmore Mar 08 '19 at 01:20
  • The answer to variable/dynamic content of any kind is "Notification Content Extension" See also https://stackoverflow.com/questions/54165040/repeating-local-notification-not-updating-content/54165157#54165157 – jscs Mar 08 '19 at 19:17

0 Answers0