首页 > 解决方案 > 在 Swift 中请求时具有未知内容的本地通知

问题描述

我正在寻找创建一个本地通知系统,它显示的文本与它在请求时包含的文本不同。我不想修改标准标题、正文格式的通知。为了测试这一点,我尝试创建一个显示当前日期的通知,但是当它显示时,显示的时间是两分钟前。是否可以在没有推送通知的情况下让此通知显示当前日期和时间(或其他可变内容)?

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)

标签: iosswiftpush-notificationnotificationsuilocalnotification

解决方案


推荐阅读