I don't know how whatsapp manages this but you can use silent notification to achieve the above feature.You need to turn on the background mode for silent notification.
When Silent notification comes it doesn't show up on screen like regular notification it will open your application even if your app in terminated mode. The system will provide some time to your app to perform certain task and you can do in that time frame only.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print(" Entire message \(userInfo)")
print("Article avaialble for download: \(userInfo["articleId"]!)")
//let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(scheduleLocalNotification), userInfo: nil, repeats: false)
let state: UIApplication.State = application.applicationState
switch state {
case UIApplicationState.active:
print("If needed notify user about the message")
default:
registerBackgroundTask()
removeBadges()
}
completionHandler(UIBackgroundFetchResult.newData)
}
func registerBackgroundTask() {
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
self?.endBackgroundTask()
}
assert(backgroundTask != UIBackgroundTaskIdentifier.invalid)
}
func endBackgroundTask() {
print("Background task ended.")
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskIdentifier.invalid
}
func removeBadge() {
UIApplication.shared.applicationIconBadgeNumber = 0
UIApplication.shared.cancelAllLocalNotifications()
}