I’m currently working on an iOS application where I need to perform certain actions (like logging) when a local notification is delivered. However, I want these actions to be executed even if the user does not interact with the notification.
I’ve done some research and found that UNNotificationServiceExtension can be used to modify notifications, but it appears to be applicable only for remote notifications, not for local notifications.
Here’s a summary of what I’ve tried and considered:
Using UNUserNotificationCenterDelegate:
I implemented the
userNotificationCenter(_:willPresent:withCompletionHandler:)
userNotificationCenter(_:didReceive:withCompletionHandler:)
methods.
These methods work well for handling notifications when the app is in the foreground or when the user interacts with the notification. However, they do not help in executing actions at the exact time when a local notification is delivered if the app is in the background and the user does not interact with it.
Possible Workarounds:
Background Fetch: Enabling background fetch and performing periodic logging. However, this does not provide the exact moment of notification delivery.
Polling in AppDelegate: Checking for delivered notifications in the applicationDidBecomeActive method and logging actions accordingly. This approach only logs the action when the app is brought to the foreground, not at the exact delivery time.