gchriswill
gchriswill

Reputation: 3

Open App from Push-Notification's Action Buttons in iOS 11+ (Swift)?

Basically open the app for which the push notification belongs to, through the notification's action buttons.

I know that tapping on the notification itself opens the app by default, but I would like to implement the same behavior to an action-button after processing action's data. I've being planning to achieve this through deep-linking with a defined URL scheme (theoretically, would work fine), but though that it might be a better approach/way. Or might not be possible at all?

Upvotes: 0

Views: 2057

Answers (2)

Karrar
Karrar

Reputation: 26

If you are creating the actions UNNotificationAction(identifier: , title:, options: UNNotificationActionOptions.foreground)

select UNNotificationActionOptions.foreground for the options parameter and it will lunch the app when the actions clicked. For more information, have a look at the Apple documentation for foreground and UNNotificationActionOptions

To add some task after lunching the app, use userNotificationCenter(_:didReceive:withCompletionHandler:) delegate method in the app delegate.

Upvotes: 1

MandisaW
MandisaW

Reputation: 1021

Are you handling the notification actions from a NotificationContentExtension, or from a UserNotificationCenterDelegate?

In iOS 10+, ContentExtensions provide a completion handler in their action-handling delegate method, didReceive(UNNotificationResponse, completionHandler: (UNNotificationContentExtensionResponseOption) -> Void). That completion handler lets you specify whether the notification should be dismissed, left-open (good for ongoing notifications, like media players), or dismissed-with-app-loading, which sounds like your case.

As of iOS 12, ContentExtension also includes a method to perform the default app-loading behavior (via its inherited NSExtensionContext reference). It's NSExtensionContext#performNotificationDefaultAction(). There's a corresponding method to silently-dismiss the notification as well.

Upvotes: 1

Related Questions