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.