Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle UNNotificationAction when app is closed?

How to handle new iOS10 Notification Action when app is closed (not in background) ?

when app is minimalized everything works fine with:

UNUserNotificationCenter.current().delegate = x

and handling it in

class x: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
    }
}

but nothing is called when app is closed and user tap action in notification... maybe i can't handle background task and i always have to launch app?

like image 424
Adam Smaka Avatar asked Sep 23 '16 07:09

Adam Smaka


3 Answers

The notification action buttons handling can be done in both Extension as well as in Containing App.

When the action button is tapped, the handle first goes to the Extension and then to the Containing App if required. If Extension does not handle the notification action, the handle is passed on to the containing app.

Tapping a button launches your app (either in the foreground or background) and gives you a chance to perform the indicated action.

Handling in extension:

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void)
{
     //You need to handle all the actions that appear with notification..
     completion(.dismissAndForwardAction)
}

The completion closure takes a value of type UNNotificationContentExtensionResponseOption:

enum UNNotificationContentExtensionResponseOption : UInt
{
    case doNotDismiss //the custom UI is not dismissed after handling the action
    case dismiss //the custom UI is dismissed after handling the action
    case dismissAndForwardAction //the custom UI is dismissed after handling the action and the control is then passed to containing app for any additional handling
}

Handling in Containing App:

extension AppDelegate : UNUserNotificationCenterDelegate
{
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
    {
        // Action handling - handling for all actions is not required
        completionHandler()
    }
}

For more you can refer to this(https://github.com/pgpt10/RichNotificationSample) tutorial.

like image 151
PGDev Avatar answered Oct 31 '22 16:10

PGDev


Yes, it always launch the app, when user tap action in notification, button launches your app. Some lines from apple doc:

Tapping a button launches your app (either in the foreground or background) and gives you a chance to perform the indicated action. You use this class to specify the text that is displayed in the button and the information your app needs to perform the corresponding action.

like image 24
Ashish Shah Avatar answered Oct 31 '22 16:10

Ashish Shah


"Tapping a button launches your app (either in the foreground or background) and gives you a chance ... " These lines appear in the doc for UIUserNotificationAction, which has been deprecated in iOS10.

The original question refers to the UNUserNotificationCenterDelegate in iOS 11. Relevant doc: Declaring Your Actionable Notification Types

Quote from the doc:

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:didReceiveNotificationResponse:withCompletionHandler: method to identify the selected action and provide an appropriate response.

like image 40
Kabeer Avatar answered Oct 31 '22 16:10

Kabeer