6

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?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Adam Smaka
  • 5,977
  • 3
  • 50
  • 55

3 Answers3

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.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • 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 `UNUserNotificationCenterDelegate` – mfaani Sep 04 '20 at 13:13
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.

Ashish Shah
  • 2,361
  • 1
  • 20
  • 20
  • 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? – Adam Smaka Sep 23 '16 at 15:00
  • 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. – Ashish Shah Sep 24 '16 at 12:13
  • but simulator does it. and we are talking about local notification – Adam Smaka Sep 25 '16 at 09:28
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.

Kabeer
  • 84
  • 5