如何在点击通知后显示某个视图控制器并发送数据?

时间:2018-03-12 06:47:27

标签: ios swift firebase push-notification firebase-cloud-messaging

我试图在这里阅读类似的帖子How to make your push notification Open a certain view controller? 但信息不完整。

我正在尝试实施推送通知(firebase cloud messaging)。收到通知提醒后,我想如果用户点击该通知,它会将用户发送到某个视图控制器并打开从服务器发送的数据。

  
      
  1. 如何从通过推送通知发送的服务器访问数据/信息?
  2.   
  3. 将该数据/信息发送给某个视图控制器?
  4.   

这是我在app delegate中的代码

import UIKit
import Firebase
import UserNotifications


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

    var window: UIWindow?
    var fcmTokenUser : String?


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

        FirebaseApp.configure()


        print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)


        // To get FCM token that will be sent to APNS via Google FCM
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()


        Messaging.messaging().delegate = self
        let token = Messaging.messaging().fcmToken
        fcmTokenUser = token




        return true
    }



    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
         // This callback is fired at each app startup (when the user install the app for the very first time) and whenever a new token is generated due to The app is restored on a new device, The user uninstalls/reinstall the app, The user clears app data.

        // after fcm generated for the very first time,then fcm can also be retrieved in the 'didFinishLaunchingWithOptions' method above (let token = Messaging.messaging().fcmToken)


        fcmTokenUser = fcmToken





    }




    private func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        Messaging.messaging().apnsToken = deviceToken as Data
    }





}

2 个答案:

答案 0 :(得分:0)

将此代码添加到FirebaseListObservable以检测用户.do()(响应),双击后会显示您指定的某个AppDelegate

tap

要从通过viewController -

发送的服务器访问func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { //THIS PRINT WILL SHOW YOU THE USER TAP ON NOTIFICATION print("userNotificationCenter --- \(response) --- ") let sb = UIStoryboard(name: "Main", bundle: nil) let otherVC = sb.instantiateViewController(withIdentifier: "yourViewControllerIdentifier") as! yourViewControllerClassName window?.rootViewController = otherVC; }

通知数据会在data/information中传送到您的应用。如果您要在push notification中处理它,则应将其存储在application:didReceiveRemoteNotification:中并在applicationDidBecomeActive:中再次阅读

答案 1 :(得分:0)

委托方法  在用户点击显示通知后处理通知消息。

 func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {

    //get required values from data
    completionHandler();
//push to view controller

}

注意:在解析后将此字典数据保存在模型中,或者在项目级别保存字典对象,以便在项目的任何位置使用。 比之前推送到任何视图控制器声明字典或模态控制器以哪种方式你想看到那里的日期。