title: Firebase token is generated every time new
body: <p>We are using Firebase Cloud Messaging in our app to displaying push notifications. According to <a href="https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId" rel="nofollow noreferrer">FirebaseInstanceId</a> doc, Instance ID is stable except when:</p>
  <ul>
  <li>App deletes Instance ID</li>
  <li>App is restored on a new device</li>
  <li>User uninstalls/reinstall the app</li>
  <li>User clears app data</li>
  </ul>
  <p>However every time we launch the app (previously stopped, not resumed), a different Token is returned through the <em>FirebaseInstanceIdService</em> <em>onTokenRefreshed()</em> callback.</p>
  <p>I was wondering if this is the normal behaviour of the service or there is a bug in the code.</p>
  <hr />
  <h2>Update</h2>
  <p>Dependency in <strong>root build gradle</strong> file:</p>
  <pre><code>classpath 'com.google.gms:google-services:3.0.0'
  </code></pre>
  <p>Dependency in <strong>app build gradle</strong> file:</p>
  <pre><code>&quot;com.google.firebase:firebase-messaging:9.2.1&quot;
  &quot;com.google.android.gms:play-services-base:9.2.1&quot;
  
  // defined at the bottom of the same file: plugin for firebase
  apply plugin: 'com.google.gms.google-services'
  </code></pre>
  <p><strong>FirebaseInstanceIdService</strong>:</p>
  <pre><code>@Override
  public void onTokenRefresh() {
    // Get the saved token from the shared preferences
    final String oldToken = PrefsHelper.getStringValue(PREF_DEVICE_TOKEN);
         
    Log.d(TAG, &quot;Old token: &quot; + oldToken);
          
    // Get updated InstanceID token.
    final String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  
    Log.d(TAG, &quot;Refreshed token: &quot; + refreshedToken);
  
    if (TextUtils.isEmpty(oldToken)) {
      handleNewToken(refreshedToken);
    } else {
      handleTokenUpdate(oldToken, refreshedToken);
    }
  }
  </code></pre>