【发布时间】:2018-12-03 11:02:28
【问题描述】:
如何在应用程序在后台运行时发送本地通知?在 Swift 4 中。我的应用程序持续使用 Json 文件,我希望应用程序继续运行,以便向用户发送本地通知。我想要一个 if label = label2 在后台,用户的应用推送通知。
label.text = myString
if label.text! == label2.text! {
let content = UNMutableNotificationContent()
content.title = "\(label2.text!)Değeri Kaydedildi!"
content.body = "Değer döndüğünde haber verilecek."
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "deneme", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
【问题讨论】:
-
对于可能遇到此问题的其他任何人..您不应该在后台进行 UI 工作。您的应用程序在后台运行后只会运行很短的时间,然后将被暂停。这意味着此代码将不会运行。您可以使用远程、静默通知来触发更新或后台获取,然后根据数据发送通知......而不是 UI
标签: ios swift usernotifications