Append Firebase Token to start URL -Trusted Web Activity

429 views Asked by At

I would like to pass FCM token in the start url. My code doesnt work everytime, i think needs a delay but i cant handle it. Below code doesnt work every time because sometimes the TWA launches before the firebase connection has been made:

    public class LauncherActivity
extends com.google.androidbrowserhelper.trusted.LauncherActivity {
public static String x = null;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//RegisterToTopic for FCM
FirebaseMessaging.getInstance().subscribeToTopic("all");
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener() {
@override
public void onComplete(@nonnull Task task) {
// Get new FCM registration token
x = task.getResult();
}
});
}

@Override
protected Uri getLaunchingUrl() {
    // Get the original launch Url.
    Uri uri = super.getLaunchingUrl();
       // Append the extra parameter to the launch Url
    return uri
            .buildUpon()
            .appendQueryParameter("z", String.valueOf(x))
            .build();
}
}

I have also tried this but the same result:

public class StartActivity extends AppCompatActivity {

    final long SPLASH_DELAY = 4000; 
    public static String x = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        runMainApp();

        FirebaseMessaging.getInstance().getToken()
                .addOnCompleteListener(new OnCompleteListener<String>() {
                    @Override
                    public void onComplete(@NonNull Task<String> task) {
                        // Get new FCM registration token

                        x = task.getResult();
                        Intent intent = new Intent(getBaseContext(), CustomLauncherActivity.class);
                        intent.putExtra("EXTRA_SESSION_ID", x);
                        startActivity(intent);


                    }
                });

    }



    private void runMainApp() {

        new Handler().postDelayed(() -> {

                    startActivity(new Intent(SplashActivity.this, CustomLauncherActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
                    finish();
                    overridePendingTransition(R.anim.anim_right_in, R.anim.anim_left_out);
                }, SPLASH_DELAY);

    }

}

I have received an answer from android-browser-helper repo but i cant handel it. If someone could provide more help would be much appreciated.

   public class MyLauncherActivity extends LauncherActivity {
  private static class DelayedTwaLauncher extends TwaLauncher {
    @Override
    public void launch(TrustedWebActivityIntentBuilder twaBuilder,
                         CustomTabsCallback customTabsCallback,
                         @Nullable SplashScreenStrategy splashScreenStrategy,
                         @Nullable Runnable completionCallback,
                         FallbackStrategy fallbackStrategy) {
    if (firebase has finished loading) {
      super.launch(twaBuilder, customTabsCallback, splashScreenStrategy, fallbackStrategy);
    } else {
      // Save the parameters to some variables.
      // Don't do anything else.
    }
  }

  public void actuallyLaunch() {
    if (we didn't call super.launch before) {
      super.launch(the parameters you saved before);
    }
  }

  @Override
  protected TwaLauncher createTwaLauncher() {
    return delayedTwaLauncher;
  }
}
1

There are 1 answers

0
andreban On

Starting with android-browser-helper] v2.2.0, it's possible to run asynchronous code before in the LauncherActivity before launching the Trusted Web Activity.

This is how a custom LauncherActivity for Firebase Analytics looks like:

public class FirebaseAnalyticsLauncherActivity extends LauncherActivity {
    private String mAppInstanceId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(this);

        // Start the asynchronous task to get the Firebase application instance id.
        firebaseAnalytics.getAppInstanceId().addOnCompleteListener(task -> {
                // Once the task is complete, save the instance id so it can be used by
                // getLaunchingUrl().
                mAppInstanceId = task.getResult();
                launchTwa();
        });
    }

    @Override
    protected boolean shouldLaunchImmediately() {
        // launchImmediately() returns `false` so we can wait until Firebase Analytics is ready
        // and then launch the Trusted Web Activity with `launch()`.
        return false;
    }

    @Override
    protected Uri getLaunchingUrl() {
        Uri uri = super.getLaunchingUrl();
        // Attach the Firebase instance Id to the launchUrl. This example uses "appInstanceId" as
        // the parameter name.
        return uri.buildUpon()
                .appendQueryParameter("appInstanceId", mAppInstanceId)
                .build();
    }
}

Check out the full Firebase Analytics demo here.