检测用户点击推送通知

时间:2018-03-26 17:28:49

标签: ios swift push-notification

如果用户单击该图标,则应用程序将从开始页面开始。如何确保用户单击推送通知以使用其他起始页启动应用程序?

2 个答案:

答案 0 :(得分:2)

func application(_ application: UIApplication, didReceive notification: UILocalNotification) {

}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken;
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Registration failed!")
}

// Firebase notification received
// This function will work app is in foreground state
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {

    // custom code to handle push while app is in the foreground
    if notification.request.content.userInfo["cook"] != nil{
        return;
    }
    print(notification.request.content.userInfo)
    UIApplication.shared.applicationIconBadgeNumber = 1;
    DispatchQueue.main.async {
       // add some code here..
       // window?.rootViewController = LoginController()

       // If you use the storyboard, then you can get controller by
       // storyboard?.instantiateViewController(withIdentifier: "identifier") as! UIViewController
    }
}

// This function will work app is in state of background or closed.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // if you set a member variable in didReceiveRemoteNotification, you  can check if this is from closed or background
    print("Handle push from background or closed\(response.notification.request.content.userInfo)")
    if response.notification.request.content.userInfo["somevariable"] != nil{
        return;
    }
    UIApplication.shared.applicationIconBadgeNumber = 1;
    DispatchQueue.main.async {
       // add some code here..
    }

}

请将此代码用于AppDelegate.swift

  

请参阅名为userNotificationCenter的功能和注释   代码。

答案 1 :(得分:1)

Ok..i会让你变得简单。

第一步 -

AppDelegate文件中添加此功能。

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

      print("Hi Kirill...")

    }

如果您在AppDelegate文件中添加此内容,则在点击通知后运行应用后,您会在调试器上看到此消息。 像这样 -

enter image description here

最后,要在应用程序中显示特定页面,请在函数内添加此内容。

let sb = UIStoryboard(name: "Main", bundle: nil)
        let otherVC = sb.instantiateViewController(withIdentifier: " Your view controller identifier name") as! yourViewControllerClassName 
        window?.rootViewController = otherVC;

看起来像这样 -

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

          print("Hi Kirill...")

          let sb = UIStoryboard(name: "Main", bundle: nil)
          let otherVC = sb.instantiateViewController(withIdentifier: " Your view controller identifier name") as! yourViewControllerClassName 
          window?.rootViewController = otherVC;

        }

点击通知时,请务必添加另一个viewControllerredirect/show。谢谢。