Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open URL from notification action handler

I am trying to handle local notifications by opening a URL on iOS 10 / Swift 3. I have one URL assigned to the notification's default action and another assigned to a custom action button.

When the default action is triggered (tapping on the notification itself), the app opens the URL successfully. But when the action button is tapped, the UNUserNotificationCenterDelegate handler is called and canOpenURL returns true, but the call to open the URL fails.

This issue happens only if the app is in the background or terminated when the notification is tapped. If the app is in the foreground both cases work.

I also have a breakpoint in my app delegate's application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) method. It hits for the default action but not for the custom action.

Do I need to do something differently when handling a custom action?

Here is the code:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
    switch response.actionIdentifier {
    case UNNotificationDefaultActionIdentifier:
        if let link = URL(string: "myapp://default"),
                    UIApplication.shared.canOpenURL(link) {
                    UIApplication.shared.open(link) { result in
                        print("open url result: \(result)") // this works!
            }
        }

    case "customActionIdentifier":
        if let link = URL(string: "myapp://custom"),
                    UIApplication.shared.canOpenURL(link) {
                    UIApplication.shared.open(link) { result in
                        print("open url result: \(result)") // this fails!
            }
        }
    }

    completionHandler()
}
like image 804
ewerx Avatar asked Apr 10 '17 23:04

ewerx


People also ask

How do I handle actionable notification types?

If your app supports actionable notification types, you must handle the associated actions. You declare actionable notification types at launch time at the same time you declare your app’s supported categories. For more information, see Declaring Your Actionable Notification Types.

Why can't I open the notification panel or Action Center?

I understand you're having trouble opening your notification panel or action center. Launch Command Prompt as an administrator and run the System File Checker and the Deployment Image Servicing and Management command to scan your hard disk for errors.

Why isn’t my app running when receiving PushKit notifications?

If your app isn’t running, the system launches your app in the background so that it can process the notification. To send a PushKit notification, your provider server must set the notification’s topic to the appropriate target, such as your app’s complication. For more information about registering for PushKit notifications, see PushKit.

How are PushKit-type notifications delivered?

If you registered your app with PushKit, notifications targeting PushKit-types are always delivered directly to your app and are never displayed to the user. If your app is in the foreground or background, the system gives your app time to process the notification.


Video Answer


1 Answers

I finally found the problem: when declaring the UNNotificationAction, you have to include UNNotificationActionOptions.foreground in the options array.

like image 188
ewerx Avatar answered Sep 20 '22 20:09

ewerx