title: How do you retrieve the messaging token for Firebase Cloud Messaging in swift after the initial boot of the app?
body: <p>I am working on integrating Firebase Cloud Messaging to my iOS app (swift). After following the documentation and some tutorials, I can register the token and print it to the log, and save it to my database, if the user is logged in. If this happens before the user signs in, either because they have to register a new account or this is a fresh install and they have not logged in yet, the app will crash immediately on start up when we find the token then attempt to write it to the database with a nil user. This is easily avoidable with a check for a nil user, which is represented in the following code from my AppDelegate</p>
  <pre><code>  func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
      print(&quot;Firebase registration token: \(String(describing: fcmToken))&quot;)
      
      let dataDict:[String: String] = [&quot;token&quot;: fcmToken ?? &quot;&quot;]
      NotificationCenter.default.post(name: Notification.Name(&quot;FCMToken&quot;), object: nil, userInfo: dataDict)
  
      if FirebaseAuth.Auth.auth().currentUser != nil{
          //  Write to the database
      } else {
          //  Save the token outside appDelegate to then update DB after sign in?
      }
    }
  </code></pre>
  <p>I attempted to solve my issue inside the <em>else</em> without success, but I still think that would be a bandaid fix, which gets to the core of my question: How can I access the token outside this function? I see the code from the docs included</p>
  <pre><code>    let dataDict:[String: String] = [&quot;token&quot;: fcmToken ?? &quot;&quot;]
      NotificationCenter.default.post(name: Notification.Name(&quot;FCMToken&quot;), object: nil, userInfo: dataDict)
  </code></pre>
  <p>Which implies I can access dataDict from anywhere. How can I access dataDict from somewhere outside of appDelegate? Or perhaps how to get to fcmToken directly from outside appDelegate?</p>
  <p>Thank you for your time!</p>
  <p><strong><strong><strong>UPDATE</strong></strong></strong></p>
  <p>The accepted answer works, with some tweaks for type casting. Here is the working code. Adding this line pretty much anywhere in my codebase finds the token</p>
  <pre><code>        NotificationCenter.default.addObserver(self, selector: #selector(getNotification), name: Notification.Name(rawValue: &quot;FCMToken&quot;), object: nil)
  </code></pre>
  <p>which calls this function (selector), within which I can store the value of token, print it to the log, or in my case update my database with its value.</p>
  <pre><code>
      @objc func getNotification(_ notification: NSNotification){
          guard let tokenDictionary = notification.userInfo as! [String:Any]? else {
              return
          }
          let token: String  = tokenDictionary[&quot;token&quot;] as! String
          print(&quot;Got TOKEN \(token)&quot;)
          // Do Stuff with token here
      }
  </code></pre>