Can notification actions be handled while the device is locked?

282 Views Asked by At

I've been working with notifications for quite a while now, and I could've sworn that userNotificationCenter(_:didReceive:withCompletionHandler:) used to only be processed if the app was opened via the notification.

However, upon looking into actionable notifications, I found this:

When the user selects an action, the system launches your app in the background and notifies the shared UNUserNotificationCenter object, which notifies its delegate. Use your delegate object's userNotificationCenter(_:didReceive:withCompletionHandler:) method to identify the selected action and provide an appropriate response.

Does this mean that the seemingly universal didReceive delegate method now works even while the device is locked?

Or must the app always be opened to actually do anything meaningful in response to notification actions (writing to local database, sending HTTPS requests, etc.)?

2

There are 2 best solutions below

1
Sachin Vas On

Yes, the delegate works even if the device is locked. The app is only woken up if the user chose an action. Also, I am not sure how much time you will get before you invoke the completion block.

Important

If your response to action involves accessing files on disk, consider a different approach. Users can respond to actions while the device is locked, which would make files encrypted with the complete option unavailable to your app. If that happens, you may need to save changes temporarily and integrate them into your app's data structures later.

0
Deviyani Swami On

From this Delegate Method : userNotificationCenter(_:didReceive:withCompletionHandler:) notification is works when your device is locked, but if you want to perform any action.

For Example: Suppose through Notification you want to pick any phone call within application then you have to setup another things as well. you have to set up PushKit Framework within your application . As shown in images: enter image description here

You have to create the different cerificate as well for PushKit Framework