title: Firebase notifications received only after relaunching app
body: <p>I have setup Firebase using Firebase assistant in Android Studio. I am facing one small, but irritating issue. The notifications doesn't work on first launch, but working on all subsequent launches. e.g.</p>
  
  <ul>
  <li>App-installed and opened for first time: Notifications are not received.</li>
  <li>If I force-stop the app and restart: Notifications are working now.</li>
  </ul>
  
  <p>I suspect that subscription request is not working on first request. 
  following is my token refresh code in android FirebaseInstanceIdService:</p>
  
  <pre><code> @Override
   public void onTokenRefresh() {
          String refreshedToken = FirebaseInstanceId.getInstance().getToken();
          if(refreshedToken != null){
              Log.d(TAG, "Refreshed token: " + refreshedToken);
              FirebaseMessaging.getInstance().unsubscribeFromTopic("news"); 
              sendTokenToServer(refreshedToken);
              FirebaseMessaging.getInstance().subscribeToTopic("news");
          }
      }
  </code></pre>
  
  <p>I do get the registration token in first launch and it is sent to server successfully. But the notification is received only after restarting the app. 
  Any ideas why it is not working in first launch?</p>
  
  <p>Edited code as suggested in comments. I have saved new token to sharedPrefs and called the Subscription function from activity. BUT THIS IS STILL NOT WORKING. App getting notification only after relaunching the app.</p>
  
  <pre><code> @Override
      public void onTokenRefresh() {
         String refreshedToken = FirebaseInstanceId.getInstance().getToken();
         if(refreshedToken != null){
              Log.d("FCGM", "Refreshed token: " + refreshedToken);
              sendTokenToServer(refreshedToken);
              myPrefs settings = new myPrefs(); //my class to handle prefs, working fine
              settings.putBool("isNewToken",true);
              settings.putString("token",refreshedToken);
          }
      }
  </code></pre>
  
  <p>then, in my activity onCreate function, i called the subscription function after a 5 sec delay to make sure that the token is generated and saved.</p>
  
  <pre><code>new Timer().schedule(new TimerTask() {
    @Override
    public void run() {
      myPrefs settings = new myPrefs();
      if (settings.getBool("isNewToken")){
          Log.d(TAG,"new token found");  //this is reaching 
          FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
          FirebaseMessaging.getInstance().subscribeToTopic("news");
          settings.putBool("isNewToken",false);
      }
    }
  }, 5000);
  </code></pre>