0

currently I am using below-mentioned code

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
    completionHandler(.alert)
    let aps = notification.request.content.userInfo[AnyHashable("aps")] as! NSDictionary
    let alert = aps["alert"] as! String
    if alert == "Welcome to XYZ. To place order please click here"{
        let stryBrd = UIStoryboard(name: "Main", bundle: nil)
        let vc = stryBrd.instantiateViewController(withIdentifier: "viewController")  as! viewController
        self.window?.rootViewController = vc
    }else{
        print("false")
    }
}

But in this code app is running automatically to desired view controller whereas I want it to happen only if a user taps on the notification.

Whereas it is working fine when an app is in the background. It's only happening if we are clicking on the notification

It should be the same as WhatsApp. When you are chatting with Mr ABC and miss PQR sends you a text then it will show push but won't do anything unless you tap on the push to open Miss PQR chat

  • Possible duplicate of [Getting local notifications to show while app is in foreground Swift 3](https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3) – stevenpcurtis Sep 26 '18 at 06:06
  • Answered here: https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3 – stevenpcurtis Sep 26 '18 at 06:07
  • No, I am asking a different question. I am able to display my push but unable to perform action on tap. Right now if I am using my code then it's automatically migrating from current VC to desired where as I want it to happen only if user taps on Push – Rajat Attri Sep 26 '18 at 06:18
  • let state = UIApplication.shared.applicationState to find the application state on will present – stevenpcurtis Sep 26 '18 at 06:21
  • Need to do the same like whatsapp when you are chatting with Mr ABC and miss PQR sends you text then it will show push but won't do anything unless you tap on push to open Miss PQR chat – Rajat Attri Sep 26 '18 at 06:23

1 Answers1

0

Use UIAlertController to show push notification and on tap of button write the code to perform action

    let stryBrd = UIStoryboard(name: "Main", bundle: nil)
    let vc = stryBrd.instantiateViewController(withIdentifier: "viewController")  as! viewController
    self.window?.rootViewController = vc

More explanation:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
    completionHandler(.alert)
    let aps = notification.request.content.userInfo[AnyHashable("aps")] as! NSDictionary
    let alert = aps["alert"] as! String
    if alert == "Welcome to XYZ. To place order please click here"{

        //show alert to the user after getting push notification

        let myalert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        myalert.addAction(UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in

            //perform action of push notification

            let stryBrd = UIStoryboard(name: "Main", bundle: nil)
            let vc = stryBrd.instantiateViewController(withIdentifier: "viewController")  as! viewController
            self.window?.rootViewController = vc
        })
        myalert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
            //handle cancel event
        })

        currentViewController.present(myalert, animated: true)
    }else{
        print("false")
    }
}
Arnab
  • 4,216
  • 2
  • 28
  • 50
  • Hi Arnab thanks for replying but showing pop up isn't an option here. Need to do the same like whatsapp when you are chatting with Mr ABC and miss PQR sends you text then it will show push but won't do anything unless you tap on push to open Miss PQR chat – Rajat Attri Sep 26 '18 at 06:20
  • then use custom view like whatsapp to show the text of push notification and add tap action to that view. On that tap action selector use the action to set rootViewController – Arnab Sep 26 '18 at 06:23
  • Look at [this](https://cloud.githubusercontent.com/assets/14218787/15983592/e208537c-2fe7-11e6-85d5-55ce0abfc798.gif) and [this](https://github.com/trilliwon/JDropDownAlert) for more understanding – Arnab Sep 26 '18 at 06:25
  • unable to open link. Access denied – Rajat Attri Sep 26 '18 at 06:26
  • search for `NotificationBanner in iOS` or `dropdown banner in iOS` in google – Arnab Sep 26 '18 at 06:27