0

我想在开始 url 中传递 FCM 令牌。我的代码每次都不起作用,我认为需要延迟,但我无法处理。下面的代码每次都不起作用,因为有时 TWA 会在 firebase 连接之前启动:

    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();
}
}

我也试过这个,但结果相同:

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);

    }

}

我收到了来自 android-browser-helper repo 的答复,但我无法处理它。如果有人可以提供更多帮助,将不胜感激。

   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;
  }
}
4

1 回答 1

1

android-browser-helper]v2.2.0 开始,可以在启动受信任的 Web 活动之前在 LauncherActivity 中运行异步代码。

Firebase Analytics 的自定义 LauncherActivity 如下所示:

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();
    }
}

在此处查看完整的 Firebase Analytics 演示。

于 2021-01-23T10:01:24.407 回答