Zyfe3r
Zyfe3r

Reputation: 653

How to open a Viewcontroller depending on the type of Push Notification?

I have push notification enabled in my app and there are 4 types of push notification (Type 1, 2, 3, 4). How can i make my app open different ViewControllers depending on the Type of push notification?

I tried searching stackoverflow and found several threads about opening a VC from push notification but unfortunately i couldn't find any thread about opening a VC depending on the type of push notification.

I am new to push notification setup and i don't know about this , any help is appreciated. (Thats the reason why i am unable to include any code)

Thanks

EDIT : The type of notifications are Int 1,2,3,4.

Upvotes: 0

Views: 160

Answers (3)

Mussa Charles
Mussa Charles

Reputation: 4412

Here is a useful example if you are using TabBarController having childViewControllers embedded inside a navigationController.

    /// Respond to user notification taps
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
          /// Get notification Info which can help you determine what VC to present.
        let userInfo = response.notification.request.content.userInfo
        // Based on userInfo add your logics
        // --------------

       // Now that you know which ViewController to present/push proceed as follows
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }
            /// Assumming your viewControllers are in tabbar controller named "CustomTabBarcontroller" you have to change this to your tabbar's name.
            if let mainTabBarController = self.window?.rootViewController as? CustomTabBarController {

                /// Select your desired ViewController, it can be at any position, I used index 2 as an example for my VC named ActivityViewController
                mainTabBarController.selectedIndex = 2

                /// Dismis any presented previous ViewController before we present a user profile.
                mainTabBarController.presentedViewController?.dismiss(animated: true, completion: nil)

/// Assume that you want to push to a viewController called ActivityViewController which is inside a navigationController located at index 2 of your tabbarController.
                if let activityNavigationController = mainTabBarController.viewControllers?[2] as? UINavigationController {
                    /// Now you can present or push to any detail view from your activityNavVC as you would do it in a normal way. Let's say you want to push to detail vc named "YourDetailVC", Note if you want to present you don't need to access the navigationController. 
                   activityNavigationController.pushViewController(YourDetailVC, animated: true)


              }
            }
        }



    }


Upvotes: 1

Allan Poole
Allan Poole

Reputation: 388

Always good to give some example code for us to work with. For example, we don't know what a notification "type" is - is it an int, a string, etc? Also, where is this type coming from? Is it in the userinfo of the push notification?

That said, based on your description, and assuming a notification "type" is just an integer, and that you already have the type from the notification, you could use an enum to make your type more readable, and to enable the use of a switch statement.

enum NotificationType: Int {
    case firstType
    case secondType
    case thirdType
}

func showViewController(withNotificationType type: Int) {
    guard let notificationType = NotificationType(rawValue: type) else {
        print("Unrecognised notification type")
        return
    }

    let viewController: UIViewController
    switch notificationType {
    case .firstType:
        viewController = FirstViewController()
    case .secondType:
        viewController = SecondViewController()
    case .thirdType:
        viewController = ThirdViewController()
    }

    navigationController.pushViewController(viewController, animated: true)
}

Upvotes: 1

Pavan Kotesh
Pavan Kotesh

Reputation: 144

You just need to find the top view controller and push the respective vc based on the type of push notification you received.

in Appdelegate.swift

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    handlePushNotification(userInfo as NSDictionary)
  }
  func handlePushNotification(_ userInfo: NSDictionary) {
    if
      let apsInfo = userInfo["aps"] as? NSDictionary,
      let vcType = apsInfo["type"] as? String,
    {
      // TODO: Here you do string matching to find which VC you want to push 
      // Else you can send a local notification and read it where ever necessary
    }
  }

Upvotes: 1

Related Questions