Hello Friends,
Could you please help me, whats wrong.
We have requirement that, when I receive remote notifications on device, I need to save it in to db (without tapping or opening notification banner), regardless my app is in Foreground or in Background.
I receive remote notifications on device & simulator both.
And
UNUserNotificationCenter delegates are being called if app is in foreground, but didReceiveRemoteNotification is not calling when app is background, not killed.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Save to DB
completionHandler(.newData)
}
I am having Xcode 15.2 & I tried on iOS versions 16.x & 17.x both on Simulator and on real devices.
// Added this code in App delegate
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
// User granted permission
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
// User denied permission
}
}