首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Firebase令牌附加到启动的-Trusted

将Firebase令牌附加到启动的-Trusted
EN

Stack Overflow用户
提问于 2020-12-06 19:40:54
回答 1查看 144关注 0票数 0

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

代码语言:javascript
运行
复制
    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();
}
}

我也试过了,但结果是一样的:

代码语言:javascript
运行
复制
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的回复,但我不能删除它。如果有人能提供更多的帮助,我们将不胜感激。

代码语言:javascript
运行
复制
   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;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-23 18:01:24

从``android browser-helper`](https://github.com/GoogleChrome/android-browser-helper) v2.2.0开始,在启动可信网络活动之前,可以在LauncherActivity中运行异步代码。

这是用于Firebase分析的自定义LauncherActivity的外观:

代码语言:javascript
运行
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65167613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档