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;
}
}
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:
Check out the full Firebase Analytics demo here.