How to handle UNNotificationAction when app is closed?

放肆的年华 提交于 2019-12-22 06:53:45

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/39655180/how-to-handle-unnotificationaction-when-app-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!