Firebase token in URL

353 Views Asked by At

I've followed this method to send my fcm token in the URL.

Following is my custom launcher activity

public class LauncherActivity
    extends com.google.androidbrowserhelper.trusted.LauncherActivity {

private String fcmToken;

@Override
protected Uri getLaunchingUrl() {
    Uri uri = super.getLaunchingUrl();
        return uri
                .buildUpon()
                .appendQueryParameter("fcmToken", fcmToken)
                .build();
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseMessaging.getInstance().getToken()
            .addOnCompleteListener(new OnCompleteListener<String>() {
                @Override
                public void onComplete(@NonNull Task<String> task) {
                    if (!task.isSuccessful()) {
                        return;
                    }
                    fcmToken = task.getResult();
                    launchTwa();
                }
            });
}

@Override
protected boolean shouldLaunchImmediately() {
        return false;
}

Problem is when i run the app for the first time it get stuck in the splash screen.Then after killing the app , second time onward it works.

This issues is discussed in here as well , but with no luck.Any help will be appreciated.

2

There are 2 best solutions below

0
On

Since i didn't find and resolution to this , following is the way i found to overcome this issue.Now i don't have the stuck in initial step and already token is passed to my server.

LauncherActivity.java

public class LauncherActivity
        extends com.google.androidbrowserhelper.trusted.LauncherActivity {

    private String fcmToken;

    @Override
    protected Uri getLaunchingUrl() {
            uri = super.getLaunchingUrl();
       
            return uri
                    .buildUpon()
                    .appendQueryParameter("fcmToken", fcmToken)
                    .build();
    }


    @Override
    protected boolean shouldLaunchImmediately() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        fcmToken = preferences.getString("fcmTokenNew", "");
        Boolean res = false;
        if(fcmToken != null && !fcmToken.trim().isEmpty()) {
            res = true;
        }
        return res;
    }


}

CustomFirebaseMessagingService.java

public class CustomFirebaseMessagingService extends FirebaseMessagingService {
    
        @Override
        public void onNewToken(String token) {
            sendToSariroti(token);
        }
    
        protected void sendToServer(String fcmToken) {
    
            try {
    
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("fcmTokenNew",fcmToken);
                editor.apply();
    
                Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(
                        getBaseContext().getPackageName() );
                intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

What is happening is when you first launch the app,

  • It checks whether the parameter(fcmToken) is not null/available inside shouldLaunchImmediately If it's available , no problem , continue with launching the app
  • If token not available stop launching the app
  • Inside onNewToken , it watches until token is received from Firebase.
  • After it received it will call sendToServer.
  • Inside sendToServer, it store the fcmToken in shared preference and re-launch the app again.

Hope this will help to someone.

0
On

This issue has been handled in version 2.2.2 of android-browser-helper. For versions before that you can call onEnterAnimationComplete() after calling launchTwa(). You can find more info about this workaround here