Opening pushnotification restarts android TWA

Viewed 18

When i send a pushnotification to my android TWA the app restarts completely, all i want to do is reopen the application. Since the notification is not handled by the TWA but through the browser is there a way to override the notification click event?

The following onCreate function gets called everytime the applications opens with or without notifation. And intentHasData returns true when i open the notification but the TWA is already restarted at that point.

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sLauncherActivitiesAlive++;
        boolean twaAlreadyRunning = sLauncherActivitiesAlive > 1;
        boolean intentHasData = getIntent().getData() != null;
        boolean intentHasShareData = SharingUtils.isShareIntent(getIntent());
        Log.e(TAG, "running: "+twaAlreadyRunning);
        Log.e(TAG, "hasData: "+intentHasData);
        Log.e(TAG, "hasShareData"+intentHasShareData);
        Log.e(TAG, "test0");
        if (twaAlreadyRunning && !intentHasData && !intentHasShareData) {
            Log.e(TAG, "test1");
            // If there's another LauncherActivity alive, that means that the TWA is already
            // running. If we attempt to launch it again, we will trigger a browser navigation. For
            // the case where an Intent comes in from a BROWSABLE Intent, a notification or with
            // share data, that is the desired behaviour.

            // However, if the TWA was originally started by a BROWSABLE Intent and the user then
            // clicks on the Launcher icon, Android launches this Activity anew (instead of just
            // bringing the Task to the foreground). In this case we don't want to launch the TWA
            // again and trigger the navigation. Since launching this Activity will have brought the
            // TWA to the foreground, we can just finish and everything will work fine.
            finish();
            return;
        }

        if(intentHasData){
            finish();
            return;
        }

        if (restartInNewTask()) {
            Log.e(TAG, "test2");
            finish();
            return;
        }

        if (savedInstanceState != null && savedInstanceState.getBoolean(BROWSER_WAS_LAUNCHED_KEY)) {
            Log.e(TAG, "test3");
            // This activity died in the background after launching Trusted Web Activity, then
            // the user closed the Trusted Web Activity and ended up here.
            finish();
            return;
        }

        mMetadata = LauncherActivityMetadata.parse(this);

        if (splashScreenNeeded() && !intentHasData) {
            Log.e(TAG, "test4");
            mSplashScreenStrategy = new PwaWrapperSplashScreenStrategy(this,
                    mMetadata.splashImageDrawableId,
                    getColorCompat(mMetadata.splashScreenBackgroundColorId),
                    getSplashImageScaleType(),
                    getSplashImageTransformationMatrix(),
                    mMetadata.splashScreenFadeOutDurationMillis,
                    mMetadata.fileProviderAuthority);
        }

        if (shouldLaunchImmediately() && !intentHasData) {
            Log.e(TAG, "test5");
            launchTwa();
        }
    }
0 Answers
Related