In my app, We have a feature to store content receive from push notification to locally core-data and are facing data loss issues on background state,
Steps we followed:
1 - Push received in didReceiveRemoteNotification method.
2 - Insert the new data from push in core data( All core data process handling in single class and single context only)
self.getManagedContext().perform {
do {
if self.getManagedContext().hasChanges {
print("Core Data class: saveChanges.....")
try self.getManagedContext().save()
}
} catch {
let saveError = error as NSError
print("Core Data class: saveChanges)
print("\(saveError), \(saveError.localizedDescription)")
}
}
3 - On screen, reading all saved list from core data and display.
Cases:
1 - Foreground : It works fine - data store and can read all data.
2 - Closed State(Exit) : User force close the app, we read data from web API to get all list based on last time stamp.
3 - Background:
-> On App run from Xcode - Debug:
We received push notification and can read push data, store in DB. But failed sometimes, we can get stored data on display (some times we can't read all data only even when app is running in XCode).
-> Open Installed Application(not run from Xcode) -> App failed to list all stored data (its obviously not stored, can't get single data for push received while background mode ).
Here two cases,
We are sure that , added content_available
and 'Background mode' enable in xcode. Please check
AnyHashable("aps"): {
alert = {
body = "Hello and 6";
title = "iPhone 6s ";
};
badge = 2;
"content-available" = 1;}
1 - App is not running on background but we are using 100% sure content_available
key added in push notification from FCM, so app should bring background mode silent push notification -> if app is suspended then the system wakes up or launches your app and puts it into the background running state
2 - App is in Suspend state - How to know app went suspend state or local DB failed to store data to main context.
We calling saveContext on going applicationDidEnterBackground
and every push received (on insert success).
Please share if any solution/any other possibility to handle push content to store locally like background fetch (but our app needs to update regularly on push received same as live chat)
Please let me know if need any more information.
Thanks!
Note: Swift 3.3
Update
In iOS 11, Push delegate method is not called on backgrund its a reason for above issue.
iOS 10 -> working fine
Not working in iOS 11 but we can able to receive push on delegate methods on debug mode.
Any idea?
[AnyHashable("gcm.notification.type"): 0, AnyHashable("gcm.notification.msg"): {"extraType":"Text","content":"Test sound"}, AnyHashable("gcm.message_id"): 0:1531860472186830%c52fb209c52fb209, AnyHashable("google.c.a.e"): 1, AnyHashable("aps"): { alert = { body = "text body"; title = "Gopi k"; }; badge = 2; "content-available" = 1; sound = Default; }]
Any idea , why delegates in not called since we have added "content-available" = 1
in push messages.
Thanks!