【问题标题】:Append Firebase Token to start URL -Trusted Web Activity附加 Firebase 令牌以启动 URL -Trusted Web 活动
【发布时间】:2021-01-23 18:01:24
【问题描述】:

我想在开始 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;
  }
}

【问题讨论】:

    标签: android firebase firebase-cloud-messaging trusted-web-activity


    【解决方案1】:

    android-browser-helper] v2.2.0 开始,可以在启动 Trusted Web Activity 之前在 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 演示 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-08
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多