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

Posted

技术标签:

【中文标题】点击通知后如何显示某个视图控制器并发送数据?【英文标题】:How to Show a Certain View Controller and Send the Data After We Tap the Notification? 【发布时间】:2018-03-12 06:47:26 【问题描述】:

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

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

    如何从服务器获取已经通过推送通知发送的数据/信息? 将该数据/信息发送到某个视图控制器?

这是我在应用委托中的代码

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
    






【问题讨论】:

你试过这个解决方案了吗?***.com/questions/33706455/… 【参考方案1】:

将此代码添加到您的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;  
    

从已经通过push notification发送的服务器访问data/information-

通知数据在application:didReceiveRemoteNotification: 中传送到您的应用程序。如果您想在applicationDidBecomeActive: 中处理它,您应该将它存储在application:didReceiveRemoteNotification: 中并在applicationDidBecomeActive. 中再次读取它

【讨论】:

【参考方案2】:

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

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

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


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

【讨论】:

以上是关于点击通知后如何显示某个视图控制器并发送数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何控制推送通知中的按钮的作用?

在 Android 中执行点击操作后,如何从通知中发送 Toast?

将触摸事件从视图发送到控制器

本地通知后 Xcode 显示特定视图

如何从控制器向 PayPal 发送数据?

iOS点击推送通知打开具体的viewcontroller