In my project Pushy used to handle Push notification. I want to update my local Sqlite data when I got a specific type of Push Notification.

I am able to handle this when application is in foreground. Using this Delegate method.

func notificationWillPresent(userInfo: [String: Any], completionHandler: ((UNNotificationPresentationOptions) -> Void)?) {
 let remoteNotification = RemoteNotificationModel(userInfo)
completionHandler?([.sound])
}

But I want this same local data updating when app is in background or in killed state. Could you please suggest me how I can Achieve this.

I did a lot of R & D and found that no method calls when app is in killed state or in background state to handle this.

1

There are 1 best solutions below

0
Piepants On

You can't do that with a user-directed push nor an app directed push.

You need to implement a notification service extension, then the user directed push will get delivered to that. The notification service extension can extract the payload and update the database.

You have probably created the database in the app's file sandbox, therefore you will need to add the app group capability to the app and to the notification extension and create the database in the shared group file sandbox so it can be accessed by both the extension and the app.

I don't know if QSLLite can handle simultaneous updates/accesses from multiple threads (or in this case it would be multiple processes), if it can then you are good to go. If it cannot then when the notification extension receives the push, it may need to determine if the app is running or not (that's possible for the extension to do using MMWormmhole), if it is running you will need to add some co-ordination between them to ensure they're not both simultaneously accessing the db.