How to handle UNNotificationAction when app is closed?
D

3

7

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?

Deina answered 23/9, 2016 at 7:34 Comment(1)
I have discovered that it happens only on my real device, simulator is ok with itDeina
M
4

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.

Minivet answered 21/2, 2017 at 10:13 Comment(1)
It took me a while to understand that the UNNotificationContentExtension is not using the typical didReceive method. Rather it's using a method with a slightly different signature. It ends with completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) and that function is exclusive to UNNotificationContentExtension ie it's not something that is to be implemented by UNUserNotificationCenterDelegateCaenogenesis
T
2

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.

Trave answered 23/9, 2016 at 14:4 Comment(3)
i have created new project and there it works. i debug it by setting notification badgeIcon = 1 and with action I make it 0 again. but in my big project the reset badgeIcon to 0, when app is closed doesn't work. nothing work, there is no action when app is closed, but in background - works. there is something wrong with my project, but what?Deina
Nothing is wrong with your app. Since push notification are handled by iOS and not your app you can't change the application badge on receiving or cancelling of a push notification in Notification on killed.Trave
but simulator does it. and we are talking about local notificationDeina
P
1

"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.

Precarious answered 21/6, 2018 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.