1

In my app, I want a URL to run when my notification action is tapped on. The problem is that when I run the app it puts me into the foreground but doesn't run the URL (I added .foreground to the notification action). Here is my code:

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == "call" {


           if let urled = URL(string: "tel://1234567891") {
                UIApplication.shared.open(urled, options: [:])                            
            }                

    }


}
}

Anyones help would be great:) Thanks for any answers!

Mannopson
  • 2,634
  • 1
  • 16
  • 32
krish
  • 159
  • 1
  • 12
  • Where does the variable `number` get set or accessed? I don't see any reference to it anywhere. – creeperspeak Feb 09 '17 at 21:23
  • @creeperspeak the variable `number` is set right above the function, I just didn't want to share any phone numbers – krish Feb 09 '17 at 21:24
  • OK, have you confirmed that the `if let urled` is passing? You can try calling `canOpenUrl` to see if the url is a valid format. Have you tried printing `"tel://\(number)"` to see if it looks right? If `number` is an optional you'll need to unwrap it first too. Could be a lot of things, but print statements and breakpoints could solve the problem for you. – creeperspeak Feb 09 '17 at 21:27
  • @creeperspeak normally I don't have a variable called `number` there, because this app is just a testing app, normally I have a typical string right there. I've tested using this url many times and it works I just can't figure out how to get it to work when the notification action is tapped on. – krish Feb 09 '17 at 21:29
  • Try `canOpenUrl` just to be sure, and put a breakpoint at `if response...` and just step through the code one line at a time to see what happens. – creeperspeak Feb 09 '17 at 21:31

3 Answers3

0

For iOS 10 : Add UserNotifications.framework to your app. Please check that you have set delegate for push notification. if you have already set it then try following method for iOS 10.

import UserNotifications

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {
}



 func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
    print("Handle push from foreground")
    // custom code to handle push while app is in the foreground
    print("\(notification.request.content.userInfo)")

}


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

 print("Handle push from background or closed")

  // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background

print("\(response.notification.request.content.userInfo)")

}
Hemant Solanki
  • 894
  • 10
  • 24
  • Please review this link for Objective C : http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10?answertab=votes#tab-top – Hemant Solanki Feb 09 '17 at 21:38
0

Try this:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

if response.actionIdentifier == "call" {

    if let urled = URL(string: "tel://\(1234567891)") {
        if UIApplication.shared.canOpenURL(urled) {
            UIApplication.shared.open(urled, options: [:], completionHandler: { (completed) in
                // completionHandler block
             }) 
         }
     } 
 }
completionHandler()}
Mannopson
  • 2,634
  • 1
  • 16
  • 32
0

I tried the same thing and I am facing the same issue.

You can try an alternative solution if you are using Notification Content Extension.

Since we can handle the notification actions in both Extension as well as Containing App, you can try handling it in Extension using extensionContext instead of Containing App's UIApplication object.

Try this:

    func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void)
    {
        if response.actionIdentifier == "call"
        {
            self.extensionContext?.open(URL(string: "tel:9555221836")!, completionHandler: { (done) in
                completion(.dismiss)
            })
        }
        //Handling other actions...
    }
PGDev
  • 23,751
  • 6
  • 34
  • 88