0

As of now i get the push notifications and displaying the banner when im in both foreground and background states. But though its works fine in the background, when I'm in foreground im unable to trigger an action to open a specific view controller once i click the banner (should open only if i click the banner). How would i do this. My code as bellow

func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {

        print("Recived: \(userInfo)")

        completionHandler(.newData)


        if state == .background {
            if mediaId != nil{
                DispatchQueue.main.async {
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let mP3ViewController = storyboard.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController
                    mP3ViewController.media_ID = mediaId
                    self.navigationController?.pushViewController(mP3ViewController, animated:true)

                }

            }
        }else if state == .active {
      //What sould i do here to get the click 

            print("Running on foreground")
            UIApplication.shared.applicationIconBadgeNumber = 0
            let count = 0
            UserDefaults.standard.setValue(count, forKey: "PUSH_COUNT")
            UserDefaults.standard.synchronize()


        }

    }
danu
  • 1,079
  • 5
  • 16
  • 48
  • if app is open push notificaiton banner wont show. as you need to use custom banner that show push notification when app on forgrond and you can get its click event – Nitin Gohel Nov 17 '17 at 10:47
  • in foreground whenever you got notification you have to dismiss all viewcontrollers from navigationcontroller and then try to push your desire one controller @danu – Muhammad Shauket Nov 17 '17 at 10:49
  • with the code above it woks fine with displaying the banner, no need to create a custom. but having troubles of triggering the click event – danu Nov 17 '17 at 10:49
  • yeah as @NitinGohel mentioned you will not get banner notification if your app is in foreground , but you will get the callback of didreceiveremotnotification – Muhammad Shauket Nov 17 '17 at 10:50
  • public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { completionHandler([.badge, .alert, .sound]) // This delegate will give my banner } – danu Nov 17 '17 at 10:52
  • Possible duplicate of [Get push notification while App in foreground iOS](https://stackoverflow.com/questions/14872088/get-push-notification-while-app-in-foreground-ios) – Kiran Sarvaiya Nov 17 '17 at 10:59

1 Answers1

1

Try this Implement UNUserNotificationCenterDelegate in your AppDelegate class

class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {
}

Add bellow method in AppDelegate class

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

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("willPresent notification")

        completionHandler([.badge, .alert, .sound])
    }

Above method will call while push notification appear in foreground.

Edit:-

Add this method called by interactive notification delegate

func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
        print("identifier = \(identifier)")
    }

Edit2 :

func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
        print("identifier = \(identifier)")
    }
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29