Firebase 通知 Swift 3 SecondViewController

Posted

技术标签:

【中文标题】Firebase 通知 Swift 3 SecondViewController【英文标题】:Firebase Notification Swift 3 SecondViewController 【发布时间】:2017-09-12 15:10:28 【问题描述】:

我在我的应用中构建了 Firebase 通知。它是成功的工作。但是如果通知数据等于“第二”,我想打开,然后在我的应用程序中打开 SecondViewController。我能怎么做?我使用 userInfo 但我没有这样做。

Firebase Notification Special Data:  "view" : "Second" 

这是我的完整 appdelegate 代码。谢谢你。对不起我的英语。

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?
    static var shared: AppDelegate  return UIApplication.shared.delegate as! AppDelegate 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
        FirebaseApp.configure()
        application.registerForRemoteNotifications()
        requestNotificationAuthorization(application: application)
        if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary 

            NSLog("[RemoteNotification] applicationState: \(applicationStateString) didFinishLaunchingWithOptions for ios9: \(userInfo)")
            //TODO: Handle background notification
        
        return true
    

    var applicationStateString: String 
        if UIApplication.shared.applicationState == .active 
            return "active"
         else if UIApplication.shared.applicationState == .background 
            return "background"
        else 
            return "inactive"
        
    

    func requestNotificationAuthorization(application: UIApplication) 
        if #available(iOS 10.0, *) 
            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)
        
    


@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate 
    // iOS10+, called when presenting notification in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
        let userInfo = notification.request.content.userInfo
        NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) willPresentNotification: \(userInfo)")
        //TODO: Handle foreground notification
        completionHandler([.alert])
    

    // iOS10+, called when received response (default open, dismiss or custom action) for a notification
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 
        let userInfo = response.notification.request.content.userInfo
        NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) didReceiveResponse: \(userInfo)")
        //TODO: Handle background notification
        completionHandler()
    


extension AppDelegate : MessagingDelegate 
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) 
        NSLog("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)")
    

    // iOS9, called when presenting notification in foreground
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) 

        NSLog("[RemoteNotification] applicationState: \(applicationStateString) didReceiveRemoteNotification for iOS9: \(userInfo)")
        if UIApplication.shared.applicationState == .active 
            //TODO: Handle foreground notification
         else 
            //TODO: Handle background notification
        
    

【问题讨论】:

如果你只是想显示......让它成为rootViewController......否则将它推入你的导航控制器......你可以直接使用window的对象。 Othewrise 在您的初始视图控制器中发布带有数据的本地 NSNotification 感谢您的回答。你能写示例代码吗? ***.com/questions/24049020/… 和 ***.com/questions/30956476/… 【参考方案1】:

来自用户信息的通知数据 在 userNotificationCenter 中添加此代码...

let str:String = (userInfo["gcm.notification.imType"] as? String)!

    switch str 
    case "FIRST":

       let rootViewController = self.window!.rootViewController as! UINavigationController

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let firstViewController = storyboard.instantiateViewController(withIdentifier: "FIRSTID") as! MessagesTableViewController

            rootViewController.pushViewController(firstViewController, animated: true)

      case "SECOND":

        let rootViewController = self.window!.rootViewController as! UINavigationController

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewController(withIdentifier: "SECONDID") as! SecondViewController

        rootViewController.pushViewController(secondViewController, animated: true)
 default:
  print("Type is something else")


【讨论】:

以上是关于Firebase 通知 Swift 3 SecondViewController的主要内容,如果未能解决你的问题,请参考以下文章

swift 3,ios 10 - 未收到推送通知 firebase

火力基地 |远程通知不显示,swift 3.0

Swift 3 Firebase 远程通知未在 TestFlight 上显示

Xcode 8.3 Swift 3 FCM 通知上的 Firebase 问题 iOS 10.3 不起作用

Swift 后台通知 Firebase

使用 Firebase 和 Swift 推送通知 [重复]