title: Firebase Dynamic Links on Android do not survive installation process
body: <p>My question is similar to <a href="https://stackoverflow.com/questions/43802446/firebase-dynamic-links-does-not-survive-installation-process">this recent question for iOS</a>.</p>
  
  <p>Firebase Dynamic links work as expected on a device with the app already existing, but I fail to get a referral when I install the app (currently in the beta channel) from the Play Store.</p>
  
  <p>Specifically, <code>AppInviteReferral.hasReferral(getIntent())</code> returns <code>false</code> when the app is installed from the PlayStore beta channel.</p>
  
  <p>According to the linked answer, Dynamic Links work most of the time, but there may be undocumented edge cases that will cause it to fail. I'll highlight what is specific to my case, so you might help me find what's missing in my setup.</p>
  
  <ol>
  <li>I only just updated my Firebase libraries to <code>10.2.6</code> from <code>10.2.4</code>. There was no change to the Firebase Invites library in the changelog. </li>
  </ol>
  
  <p>If it matters, here's the order in which I include the libraries</p>
  
  <pre><code>compile 'com.google.firebase:firebase-core:10.2.6'
  compile 'com.google.firebase:firebase-messaging:10.2.6'
  compile 'com.google.firebase:firebase-auth:10.2.6'
  compile 'com.google.firebase:firebase-database:10.2.6'
  compile 'com.google.firebase:firebase-invites:10.2.6'
  </code></pre>
  
  <ol start="2">
  <li><p>My <code>SplashScreenActivity.java</code> serves as both the launcher activity, and the activity that accepts and handles deeplinks. Here's the activity declaration in the AndroidManifest</p>
  
  <pre><code>&lt;activity
      android:name=".ui.setup.SplashScreenActivity"
      android:label="@string/app_name"
      android:theme="@style/SplashTheme"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.MAIN" /&gt;
  
          &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
      &lt;/intent-filter&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.VIEW"/&gt;
          &lt;category android:name="android.intent.category.DEFAULT"/&gt;
          &lt;category android:name="android.intent.category.BROWSABLE"/&gt;
          &lt;data android:scheme="https"
              android:host="deeplinks.myCompanyDomain.com"
              android:pathPrefix="/mobile"/&gt;
      &lt;/intent-filter&gt;
  &lt;/activity&gt;
  </code></pre></li>
  <li><p><code>SplashScreenActivity.java</code> does not <code>setContentView(int id)</code>. It just uses a theme to display the splash screen while the rest of the app's resources "load". I don't know if this matters, but I'm putting it out there.</p></li>
  <li><p>Before anything starts on the app, I check to make sure the app has the needed permissions. A <code>continueIntoApp()</code> method (I couldn't think of a better name) takes the user into the app when it finds it has the needed permissions, or after the user grants the app all four permissions it needs.</p></li>
  <li><p><code>continueIntoApp()</code> is where all the code found on the <a href="https://firebase.google.com/docs/dynamic-links/android/receive" rel="nofollow noreferrer">Firebase Dynamic Links Docs</a> is implemented. I first build and connect a <code>GoogleApiClient</code>.</p>
  
  <pre><code>GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
          .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
              @Override
              public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                  LogUtils.e("Deeplink connection failed");
                  LogUtils.e(connectionResult.getErrorMessage());
                  LogUtils.e(String.valueOf(connectionResult.getErrorCode()));
              }
          })
          .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
              @Override
              public void onConnected(@Nullable Bundle bundle) {
                  LogUtils.d("Connected!");
              }
  
              @Override
              public void onConnectionSuspended(int i) {
                  LogUtils.e("Connection suspended!");
              }
          })
          .addApi(AppInvite.API)
          .build();
  
  googleApiClient.connect();
  </code></pre></li>
  </ol>
  
  <p>Just as an aside, the Dynamic Links docs assume the developer already knows how to setup a <code>GoogleApiClient</code>. I didn't. After a few frustrating days, I accidentally found the <code>#connect()</code> method that actually got the <code>GoogleApiClient</code> doing what it was supposed to do.</p>
  
  <p>After this, I check if the <code>AppInviteReferral</code> has a referral.</p>
  
  <pre><code>    //boolean autoLaunchDeepLink = true;
      if(AppInviteReferral.hasReferral(getIntent())){
          LogUtils.d("Referral found!");
          AppInvite.AppInviteApi.getInvitation(googleApiClient, SplashScreenActivity.this, true)
                  .setResultCallback(new ResultCallback&lt;AppInviteInvitationResult&gt;() {
                      @Override
                      public void onResult(@NonNull AppInviteInvitationResult appInviteInvitationResult) {
  
                          LogUtils.d("Processing appInviteInvitationResult...");
  
                          if(appInviteInvitationResult.getStatus().isSuccess()){
                              Intent intent = appInviteInvitationResult.getInvitationIntent();
                              String deepLink = AppInviteReferral.getDeepLink(intent);
  
                              LogUtils.d("Deeplink is " + deepLink);
                              AppConfig appConfig = new AppConfig(SplashScreenActivity.this);
                              appConfig.put(ModelKeys.TEMP_JOIN_BRANCH_DEEPLINK, deepLink);
                              startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
                              //parseDeeplink(deepLink);
                          }else {
                              LogUtils.d("No deeplink found!");
                              startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
                          }
                      }
                  });
      }else {
          LogUtils.d("No referral found!");
          startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
      }
  </code></pre>
  
  <p>You will notice I have commented out <code>autoLaunchDeepLink</code> and, by default, pass <code>true</code> to <code>AppInvite.AppInviteApi.getInvitation()</code>. I'm still not sure when I should set this value to <code>true</code> or <code>false</code>. I also don't know how, after a fresh installation from a Dynamic Link (with <code>autoLaunchDeepLink</code> as <code>false</code>), Firebase knows how to "start the dynamic link".</p>
  
  <p>That's as far as the Dynamic Links implementation goes. My problem is as stated above: when I have the app already installed, <code>AppInviteReferral.hasReferral(getIntent())</code> returns <code>true</code> , and the code runs as normal. When the user follows the Dynamic Link to the PlayStore and downloads the beta release, <code>AppInviteReferral.hasReferral(getIntent())</code> returns <code>false</code>, and the deeplink is not followed.</p>
  
  <p>Why is this happening? What am I missing?</p>