使用推送通知快速打开特定视图

时间:2019-08-25 15:59:43

标签: ios swift push-notification

我有一个用户可以接收推送通知的应用程序。该推送通知可以正常工作并到达设备。如果应用程序上有新用户或当用户添加软件包时,通知将发送到用户设备。如何单击“通知”以转到新的用户配置文件,而不仅仅是打开应用程序。以下是我如何将推送通知发送到设备的信息。

extension AppDelegate: UNUserNotificationCenterDelegate {

    func delegateSetup(_ application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {

        // For iOS 10+ display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        // For iOS 10+ data message (sent via FCM)
        Messaging.messaging().delegate = self

        application.registerForRemoteNotifications()

        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "no token")")

        Messaging.messaging().shouldEstablishDirectChannel = true

        let option: UNAuthorizationOptions = [.badge, .sound, .alert]
        UNUserNotificationCenter.current()
            .requestAuthorization(options: option) { success, error in
                if let error = error {
                    print("Error: \(error)")
                }
        }

        if launchOptions?[.remoteNotification] != nil {
//            window?.rootViewController = NavigationManager.instantiateSignupViewController()
        } else {
//            window?.rootViewController = NavigationManager.instantiateSplashViewController()
        }
        if let token =  Messaging.messaging().fcmToken {
            //Send token to server if neccessary
            sendTokenToServer(token)
        }
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
        let token = tokenParts.joined()
        print("FirebaseTest Device Token: \(token)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("FirebaseTest Failed to register: \(error)")
    }

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
        log("FirebaseTest 444 \(notification.request.content.userInfo)")
        completionHandler([.alert, .badge, .sound])
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // Print full message.
        log("FirebaseTest  \(userInfo)")
        if application.applicationState == .active {
            log("FirebaseTest App already open")
        } else {
            log("FirebaseTest App opened from Notification")
        }
        completionHandler(.newData)
    }

}

感谢您的帮助

响应通知的示例是

"status": true,
    "message": "Notification sent",
    "data": [
        "{\"multicast_id\":5626624406944416203,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"1566407814132865\"}]}",
        {
               "body" : "A new property has just been approved under your agency",
               "title" : "New property Approved",
               "type" : "new_property",
               "meta"  : {"property_id" : 4}
           }
    ]

property_id是用来显示细节的

1 个答案:

答案 0 :(得分:0)

在通知中心中单击通知时,打开应用程序时会调用此方法

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { 
   let userInfo = response.notification.request.content.userInfo
   print(userInfo)
}

您可以从此通知信息中延迟显示另一个vc的过程,直到应用程序处于活动状态