首页 > 解决方案 > 如何根据推送通知的类型打开 Viewcontroller?

问题描述

我在我的应用程序中启用了推送通知,并且有 4 种类型的推送通知(类型 1、2、3、4)。如何让我的应用根据推送通知的类型打开不同的 ViewController?

我尝试搜索 stackoverflow 并找到了几个关于从推送通知中打开 VC 的线程,但不幸的是,根据推送通知的类型,我找不到任何关于打开 VC 的线程。

我是推送通知设置的新手,对此我一无所知,不胜感激。(这就是我无法包含任何代码的原因)

谢谢

编辑:通知的类型是 Int 1,2,3,4。

标签: swiftpush-notificationapple-push-notifications

解决方案


您只需要找到顶视图控制器并根据您收到的推送通知类型推送相应的 vc。

在 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
    }
  }

推荐阅读