title: iOS Swift Firebase InstanceID token returns Nil at first time
body: <p>I am using Firebase notification in my app. When I install my app for first time <code>FIRInstanceID.instanceID().token()</code> returns nil, but it won't return nil at next time. Everything is done perfect except this.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import UIKit
  import Firebase
  import FirebaseMessaging
  import FirebaseInstanceID
  class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate
  {
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool
  {
          //Configuring Firebase
          FIRApp.configure()
  if #available(iOS 10.0, *)
          {
              print("Test")
              // For iOS 10 display notification (sent via APNS)
              UNUserNotificationCenter.current().delegate = self
              UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
                  if granted
                  {
                      //self.registerCategory()
                  }
              }
              // For iOS 10 data message (sent via FCM)
              FIRMessaging.messaging().remoteMessageDelegate = self
          }
          else
          {
              let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
              application.registerUserNotificationSettings(settings)
          }
  
          application.registerForRemoteNotifications()
  NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil)
          return true
  }
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
      {
          FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
          FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod)
      }
  func tokenRefreshNotification(notification: NSNotification)
      {
          if let refreshedToken = FIRInstanceID.instanceID().token()
          {
              print("InstanceID token: \(refreshedToken)")
          }
          // Connect to FCM since connection may have failed when attempted before having a token.
          connectToFcm()
      }
  
      func connectToFcm()
      {
          FIRMessaging.messaging().connect { (error) in
              if (error != nil)
              {
                  print("Unable to connect with FCM. \(error)")
              }
              else
              {
                  print("Connected to FCM.")
              }
          }
      }
  }
  </code></pre>
  
  <p>In Remote Page I am Calling Firebase InstanceID Token</p>
  
  <pre><code>let token = FIRInstanceID.instanceID().token()
  </code></pre>
  
  <p>But at first time it return nil value.</p>