title: Firebase token is null when I run the app for the first time
body: <p>I'm trying to retrieve a firebase token, but when I ran the app for the first time, the firebase token was null. I don't know why. When I ran the app for the second time I got the token. I saw another post with a similar question but I didn't understand it. </p>
  
  <p>This is my code : </p>
  
  <pre><code>public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
  private static final String LOGTAG = "android-fcm";
  
  @Override
  public void onTokenRefresh() {
      String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  
      storeTokenInPref(refreshedToken);
  
  }
  
  
  private void storeTokenInPref(String token) {
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString("regId", token);
      editor.apply();
  }
  </code></pre>
  
  <p>I have a fragment where you can get the token of preferences, and then I want to compare if token is inside the database.</p>
  
  <pre><code>public class Profile extends Fragment implements MainActivity.OnBackPressedListener{
  
  private View view;
  
  private  SharedPreferences preferences;
  
  private String token;
  
  private static final String LOGTAG = "android-fcm";
  
  public Profile(){}
  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      view = inflater.inflate(R.layout.profile, container, false);
  
      ((MainActivity) getActivity()).setOnBackPressedListener(this);
  
      preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
  
      displayFirebaseToken();
      return view;
  }
  
  private void displayFirebaseToken() {
  
      token = preferences.getString("regId", null);
      Log.e(LOGTAG, "Firebase reg id: " + token);
  
      if (!TextUtils.isEmpty(token)) {
  
          SQLiteHelper database = new SQLiteHelper(getActivity(), "DB", null, 1);
  
          List&lt;Beacon&gt; beaconList = database.getAllBeacons();
  
          String classroom = "";
  
          for (int i = 0; i &lt; beaconList.size(); i++) {
  
              if (beaconList.get(i).getId().equals(token)) {
                  classroom = beaconList.get(i).getClassroom();
                  break;
              }
          }
          classroomTextView.setText(classroom);
  
      }else {
          System.out.println("Firebase token is not received yet!");
      }
  }
  </code></pre>