Reputation: 8904
I'm trying to implement Background Fetch API in my app for that I've configured as below.
I've enabled Background Fetch from Capabilities.
In AppDelegate.swift
Added this in didFinishLaunchingWithOptions
method
UIApplication.shared.setMinimumBackgroundFetchInterval(30)
Implemented this method too to perform task.
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
debugPrint("New notification fired from AppDelegate...!!")
let notif = UNMutableNotificationContent()
notif.title = "New notification from App delegate"
notif.subtitle = "Cool App!"
notif.body = "I liked it!"
UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .badge, .alert], completionHandler: { (isGranted, error) in
DispatchQueue.main.async {
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: "myNotification", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil{
print(error!)
} else {
// do something
}
}
}
})
}
After configuring all the things local notification not firing. Why so?
Am I missing something?
I've also tried this tutorial
Any help will be appreciated!
Upvotes: 1
Views: 1686
Reputation: 1771
There is similar question about this:
Background Fetch Does Not Appear to Fire
Try to force it to run on the simulator, If fetch event works in simulator, that proves that everything is correctly set up. There's nothing you can do but wait.
Upvotes: 0
Reputation: 6611
You are not calling completionHandler in performFetchWithCompletionHandler
. I am able to test BackgroundFetch
with below code:
DispatchQueue.main.async {
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 6.0, repeats: false)
let request = UNNotificationRequest(identifier: "myNotification", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil{
print(error!)
completionHandler(.failed) // Add this line
} else {
completionHandler(.noData) // Add this line
}
}
}
You can test Background Fetch with below steps:
Now you will able to Test Background Fetch.
Upvotes: 1