我想在开始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;
}
}
发布于 2021-01-23 18:01:24
从``android browser-helper`](https://github.com/GoogleChrome/android-browser-helper) v2.2.0开始,在启动可信网络活动之前,可以在LauncherActivity中运行异步代码。
这是用于Firebase分析的自定义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演示here。
https://stackoverflow.com/questions/65167613
复制相似问题