I want to add background fetch for my app, when the silent remote notification arrives (with content-available : 1 ), so when it arrives, I trigger a background update locally, check for new data and if so generate a local push notification for the user. However, it works perfectly only when the app is in the background, not suspended or not running state. In those states I get it to fetch some data, but local notification is not fired (it's created as seen in console of the device), any ideas how can I solve that?
Here's my code for generating push (it works as expected)
func generatePushNotification(with title: String, body: String, badge: Int = 1){
let content = UNMutableNotificationContent()
content.badge = badge as NSNumber
content.title = title
content.body = body
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1,
repeats: false)
let notification = UNNotificationRequest(identifier: "pushLocal", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(notification) { (error) in
if error != nil {
NSLog((error?.localizedDescription)!)
}
}
}
Here's my code for notificationFetch
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
networkManager.getScheduleForGroup(group) { (resp, error) in
self.generatePushNotification(with: "Back", body: "ground")
completionHandler(.newData)
}
completionHandler(.noData)
}
Here's the console output:
dasd CANCELED: com.apple.pushLaunch.**:297454 <private>!
SpringBoard Saving notification 802A-AE8C: 0 [ hasAlertContent: 0, shouldPresentAlert: 1 ]
SpringBoard Delivered user visible push notification 802A-AE8C
SpringBoard Load 0 pending notification dictionaries
SpringBoard Adding notification 802A-AE8C to destinations 0 [ hasAlertContent: 0, shouldPresentAlert: 1 hasSound: 0 shouldPlaySound: 1 ]