IOS10 如何处理用户点击推送通知

IOS 10 how to handle user clicking on push notification

我已经设法让推送通知正常工作并使用 FireBase 转到正确的设备。但是,如果用户单击推送通知,我希望他们现在都转到一个名为 "NotificationC" 的特定控制器,如果用户单击推送通知,它会转到我不想要一个名为 HomeC 的控制器。我已阅读 Firebase push notification action 但仍然无法将其转到该控制器,这是我的代码。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.shared.registerForRemoteNotifications()
                Messaging.messaging().delegate = self as? MessagingDelegate
            }
            else{
                //Do stuff if unsuccessful...

            }
        })
        application.registerForRemoteNotifications()

        NotificationCenter.default.addObserver(self, selector:
            #selector(tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
        FirebaseApp.configure()
        return true
    }

    func tokenRefreshNotification(notification: NSNotification) {
        // NOTE: It can be nil here
        let login = LoginC()
        login.getDeviceToken(id: MYID)
        newDeviceToken = true
        }






    // The callback to handle data message received via FCM for devices running iOS 10 or above.
    func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {

        let homeC = HomeC()
        homeC.getMyNotifcations()


    }


    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("User Info = ",notification.request.content.userInfo)
        completionHandler([.alert, .badge, .sound])
    }

    //Called to let your app know which action was selected by the user for a given notification.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let homeC = HomeC()
        homeC.getMyNotifcations()
        completionHandler()
    }

上面的代码

            let homeC = HomeC()
            homeC.getMyNotifcations()

本质上是一个 segue,它应该让用户访问我的通知控制器,这就是我完成它的方式

func getMyNotifcations() {
  let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationC") as! NotificationC
    Popup.notificationCount = Int(notificationCount!)
    self.addChildViewController(Popup)
    Popup.view.frame = self.view.frame
    self.view.addSubview(Popup.view)
    Popup.didMove(toParentViewController: self)
}

当我将 getMyNotifcations 放入 IBAction 时,它起作用了。同样,我只是希望用户在点击 Notification 时转到 NotificationC Controller,任何建议都很好。我还在 AppDelegate 中的所有这些函数上设置了一个断点,并且 none 它们在点击时触发。

您的视图控制器似乎没有正确实例化,您只是在创建 class 的对象。

试试这个,也许还可以添加延迟。

    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let homeC = storyboard.instantiateViewController(withIdentifier: "YOUR_VIEWCONTROLLER_IDENTIFIER_IN_STORYBOARD") as? HomeC
    if homeC != nil {
        homeC!.view.frame = (self.window!.frame)
        self.window!.addSubview(homeC!.view)
        self.window!.bringSubview(toFront: homeC!.view)
        homeC.getMyNotifcations()
    }