【问题标题】:How do I implement deep links in local notification如何在本地通知中实现深层链接
【发布时间】:2021-11-13 19:39:30
【问题描述】:

我的应用程序中有很多屏幕。因此,当用户点击本地通知时,我想深度链接其中一个屏幕。

可能在其中一个屏幕上未运行或处于活动状态,或者处于非活动或挂起状态。

我不知道该怎么做

如果有人能解释就太好了

【问题讨论】:

  • 您应该先了解屏幕,否则您将无法导航。例如,您想导航到购物车屏幕,只需传递购物车项目和屏幕名称。
  • 你能给我一个示例代码 sn-p 将屏幕名称从通知传递到应用程序吗?
  • 检查,添加示例代码。
  • 我不是指 NSNotification .. 我的意思是本地和推送通知 .. 本地通知。
  • 编辑您的问题。仍然是相同的导航过​​程。

标签: ios uilocalnotification unusernotificationcenter


【解决方案1】:

在 appdelegate 或 viewcontroller 中继承 UNUserNotificationCenterDelegate

实现以下方法

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

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

您可以在此处拦截通知点击并使用OpenURL 或直接推送或显示或切换视图控制器的选项卡调用相应的模块。

【讨论】:

    【解决方案2】:

    您可以使用DeepLinkKit 导航到不同的屏幕。我在我的应用程序中使用这个库,应用程序中的每个屏幕都有大约 30-37 个深度链接。我将其与 Cordinators 模式一起用于导航。如果您在实施方面需要任何帮助,请告诉我。

    【讨论】:

      【解决方案3】:
      1. 从任何地方发送通知。

        func sendNotification(){
         let info: [String: String] = ["vc_name": "CartViewController"]
         NotificationCenter.default.post(name: Notification.Name("YourLocalNotification"), object: nil, userInfo: info)
        }
        
      2. 在根控制器的接收通知上添加通知处理程序。

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

      接收通知,检查您的通知信息并导航到您各自的控制器。

          @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);
             }
          }
      

      【讨论】:

      • 我正在接受本地通知
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      相关资源
      最近更新 更多