Reputation: 1295
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.
Upvotes: -1
Views: 42
Reputation: 535889
However, I want these actions to be executed even if the user does not interact with the notification
That's not possible. A notification is between the system, whose aid you have enlisted, and the user. Your app is not notified merely because the notification is triggered. As you have rightly said, if your app happens to be running and is frontmost at the time the notification is delivered, and if you have accordingly configured the notification, your app can hear about it; but otherwise, not. You need to modify your desires or consider some other mechanism entirely.
Upvotes: 0