在后台运行的应用发送本地通知

时间:2018-12-03 11:02:29

标签: ios swift usernotifications

当应用程序在后台运行时,如何发送本地通知?在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)

        }

1 个答案:

答案 0 :(得分:0)

在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)
}