0

Apple push notification comes with a completionHandler which is recommended to be called after the task finishes for background push,

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

Now what happens if I do not call this handler even after my task is finished?


handler - The block to execute when the download operation is complete. When calling this block, pass in the fetch result value that best describes the results of your download operation. You must call this handler and should do so as soon as possible. For a list of possible values, see the UIBackgroundFetchResult type.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • If you don't call it then ios will stop delivering background notifications to your app. Note the word "must" in the documentation. It isn't just a recommendation. It is a requirement that you call the completion handler. – Paulw11 Mar 29 '20 at 20:31
  • How can I save the push userInfo for later use? When push is received my db is not ready due to data protection unavailable and hence I need to store the push so that I can fire handler after protected data becomes available. Is it even possible to store handler and fire it later? – Sazzad Hissain Khan Mar 29 '20 at 21:11
  • If you need to be able to update the database when the device is locked then you will need to change the protection class on its files to something like "accessible after first unlock". Note that the delivery of remote notification is not guaranteed and it not a reliable way to deliver data; your app should always check your server for new data when it returns the foreground regardless of notifications – Paulw11 Mar 29 '20 at 21:15
  • If you don't want to change the protection class on your main database, you could save the notification data in another file/database with the appropriate protection class for later processing, but you must call the completion handler during the current background execution block; it can't be "saved" and called later – Paulw11 Mar 29 '20 at 21:18
  • @Paulw11 I have already configured data protection for `after first unlock` yet OS is not making my encrypted sql database available for sometimes. Stuck with that issue. – Sazzad Hissain Khan Mar 29 '20 at 21:19
  • @Paulw11 ok got it. You are suggesting to store the userInfo but still call the handler to let OS know that my task is done. And when protected data available I will process the stored notification data. That make sense though. – Sazzad Hissain Khan Mar 29 '20 at 21:21
  • Yes, but if you have set the protection class successfully then the only point at which the files would be unavailable is when the device is first powered on and has never been unlocked. – Paulw11 Mar 29 '20 at 21:23
  • @Paulw11 yes it is supposed to work as per doc, but we are getting the issue for userdefults and sometimes for encrypted sql db even after first unlock. Possibly a bug https://forums.developer.apple.com/message/207807#207807 – Sazzad Hissain Khan Mar 29 '20 at 21:32

0 Answers0