hypery2k / nativescript-urlhandler

NativeScript URL Handler Plugin
MIT License
52 stars 34 forks source link

handleOpenURL is called each time that the application is resumed #91

Closed pkokorev closed 4 years ago

pkokorev commented 5 years ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Current Behavior

Function handleOpenURL is called each time that the application is resumed (goes to front) with the latest deeplink consumed.

Expected Behavior

Function handleOpenURL would be called only when the deeplink is used to bring the app to front.

Failure Information

I am trying to setup a redirection to a certain page after the deeplink is clicked on. It works just fine, but when I navigate away from the deeplinked page, next time the application is resumed I get redirected back to the deeplinked page again. Looks like handleOpenURL memorizes the last used deeplink and calls the callback with it everytime the app is brought to front.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  ngOnInit() {
    console.log('Initializing app component');

    handleOpenURL((appUrl: AppURL) => {
      console.log('Opening deeplink', appUrl.path, JSON.stringify(Array.from(appUrl.params.entries())
        .reduce((object, [key, value]) => {
          object[key] = value;
          return object;
        }, {})
      ));
      return this.zone.run(() => {
        switch (appUrl.path) {
          case 'abc':
            return this.routerExtensions.navigate([
              'abc/' + appUrl.params.get('def') + '/' + appUrl.params.get('xyz')
            ], {clearHistory: true}).then(() => console.log('App :: Navigating to abc'));
          case  'xyz':
            return this.routerExtensions.navigate([
              'xyz/' + appUrl.params.get('abc')
            ], {clearHistory: true}).then(() => console.log('App :: Navigating to xyz'));
          default:
            return this.routerExtensions.navigate([
              'home'
            ], {clearHistory: true}).then(() => console.log('App :: Navigating to home'));
        }
      });
    });
}

Context

NativeScript version: 5.1.0 Java version: 1.8.0_211 NodeJS and NPM version: v8.11.4 and 6.10.1 Platform(s) running: Android

Demo Project

ℹ None...

Failure Logs

JS: Opening deeplink xyz {"abc":"123"}
JS: App :: Navigating to xyz
kennethkeim commented 5 years ago

I have the same issue, the latest deeplink is used when I'm returning to my app after selecting photos with the image-picker plugin.

kennethkeim commented 5 years ago

I just discovered the deeplink is used anytime the app resumes from the app switcher.

hypery2k commented 5 years ago

does this happens on iOS and Android or only within Android?

kennethkeim commented 5 years ago

@hypery2k It's only happening on Android for me.

dlcole commented 4 years ago

I'm seeing something similar. After receiving a deep link, any time I open a modal dialog the last deep link received is re-opened. This is occurring on Android in a plain Javascript app.

Edit: to invoke the behavior, all I need to do is tap the Overview button and select my app. Boom, the UrlHandler code is invoked again :-(

Edit: and, this is only happening on Android

tobydeh commented 4 years ago

Facing the same issue on Android - handleOpenUrl is called every time the app resumes.

arminghm commented 4 years ago

Add android:launchMode="singleTask" to the activity in AndroidManifest.xml.

netodomenico commented 4 years ago

Same issue here (Nativescript-Vue, Android). Method handleOpenUrl is called every time the app is resumed, with android:launchMode="singleTask" as well. Do you have any ideas?

luiguild commented 4 years ago

I confirm same behavior here, in Android even putting android:launchMode="singleTask" the handleOpenURL is called every time with the last url

jubinj commented 4 years ago

I am also facing the same issue. Any solutions to this?

dlcole commented 4 years ago

I hit this same problem again yesterday, and it introduces a timing problem that's difficult to bypass. I hope to investigate later today and will submit a PR if I can develop a fix.

dlcole commented 4 years ago

Here's what I've found:

The plugin handles activityStartedEvent but never clears the intent, so any time an activity starts this code is triggered and the intent processed (or re-processed).

This appears to be a common problem on Android (see this SO post) and can be remedied by clearing the intent. I modified line 14 in urlhandler.android.ts:

setTimeout(() => getCallback()(appURL));

to this:

setTimeout(() => {
  let result = getCallback()(appURL);
  intent.setAction("");
  return result; 
});   

In my initial testing this seems to work OK. The plugin is still handling the actvityStartedEvent, but because the intent action has been cleared it doesn't get passed through to my app. The SO post referenced above has several other solutions and associated warnings. I'd welcome any additional testing and feedback before I create the PR.

fpaaske commented 3 years ago

Hi! I just ran into this issue. Are there any plans to make this available as a release soon?