ios - 后台运行的应用程序发送本地通知
问题描述
应用程序在后台运行时如何发送本地通知?在 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)
}
解决方案
在 AppDelegate 中,尝试查找函数didReceiveRemoteNotification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
#if DEBUG
print("Get User Info fetchCompletionHandler \(userInfo)")
#endif
// Your notification is in userInfo, you should cast it first before you show it
// Do what you want
completionHandler(.newData)
}
推荐阅读
- matlab - Does the order of inputs matter in MATLAB functions?
- ansible - 在ansible中使用with_sequence在with_subelement上循环
- selenium - Selenium Webdrive 所需的自动化建议
- python - Python Pandas new dataframe column with group by and condition
- flutter - Flutter TextField - how to shrink the font if the text entered overflows
- css - Changing the Visual Composer CSS path to prevent https errors
- jquery - JQuery JSON 键集自动完成
- r - 我怎样才能最干净地使用 Roxygen2 来记录 R 中的向量
- html - How to support HTML dom elements with RTL / LTR languages?
- php - "SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into