Home > Software design >  How do I implement deep links in local notification
How do I implement deep links in local notification

Time:09-21

I have so many screens in my application. So when the user taps on the local notification , I'd like to deep-link one of the screens.

The could be not running or active on one of those screens or inactive or suspended state.

I couldn't see how to do that

If anyone could explain it'd be great

CodePudding user response:

You can use DeepLinkKit for navigation to different screens. I'm using this library in my app, I have about 30-37 deeplinks for every screen in the app. I'm using this with Cordinators pattern for navigation. Let me know if you need any help with implementing.

CodePudding user response:

  1. Send Notification from anywhere.

    func sendNotification(){
     let info: [String: String] = ["vc_name": "CartViewController"]
     NotificationCenter.default.post(name: Notification.Name("YourLocalNotification"), object: nil, userInfo: info)
    }
    
  2. Add Notification handler on Receive Notification in your root controller.

    override func viewDidLoad() {
         super.viewDidLoad()
         NotificationCenter.default.addObserver(self, selector: #selector(self.youNotificationAction(notification:)), name: Notification.Name("YourLocalNotification"), object: nil)
     }
    

Receive notification, Check your notified information and navigate to your respective controller.

    @objc func youNotificationAction(notification: Notification) {
      if let vcName = notification.userInfo?["vc_name"] as? String {
          let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
          let toVC = storyBoard.instantiateViewController(withIdentifier: vcName)
          navigationController?.pushViewController(toVC, animated: true);
       }
    }
  • Related