Cordova deeplinks - query parameter not detected from Universal Links

1.6k views Asked by At

I am trying to add Universal Linking to a Cordova App using the ionic-plugin-deeplinks plugin.

According to this issue query parameters should work out of the box.

Universal Links for me work correctly except for links with query parameters.

Eg. https://my-site.com/?olddeeplinking=resetpassword&token=123


When I click on the link in an email the queryString field is always an empty string.

enter image description here

Am I missing something, do I need to enable the plugins to detect query params?

Here is the code that I'm using:

const deepLinkRoutes = {
  '/user/login': {
    action: 'showLogin',
    resetUrl: '/',
  },
  '/user/forgot-password': {
    action: 'showForgotPassword',
    resetUrl: '/',
  },
  ...
};

export const _getIonicRoutes = () => Object.keys(deepLinkRoutes)
  .reduce((links, route) => {
    links[route] = { target: '', parent: '' };
    return links;
  }, {});

export const handleUniversalLinks = () => {
  const ionicRoutes = _getIonicRoutes();
  const sy = obj => JSON.stringify(obj);
  const matchFn = ({ $link, $route, $args }) => {
    console.log('Successfully matched route', $link, $route, $args);
    alert(`Successfully matched route: ${sy($link)}, ${sy($route)}, ${sy($args)}`);
    return history.push($link.path);
  };
  const noMatchFn = ({ $link, $route, $args }) => {
    console.log('NOT Successfully matched route', $link, $route, $args);
    alert(`NOT Successfully matched route: ${sy($link)}, ${sy($route)}, ${sy($args)}`);
    return history.push($link.path);
  };
  window.IonicDeeplink.route(ionicRoutes, matchFn, noMatchFn);
};

UPDATE: It looks like the intent received on Android is always /user/login even though the Universal Link does not have it. What could be causing that?

2019-10-21 17:22:47.107 30389-30389/? D/MessageViewGestureDetector: HitTestResult type=7, extra=https://nj.us.gpd.my_company-dev.com/user/login 2019-10-21 17:22:47.139 1128-1183/? I/ActivityManager: START u0 {act=android.intent.action.VIEW dat=https://nj.us.gpd.williamhill-dev.com/... cmp=us.my_company.nj.sports.gpd/.MainActivity} from uid 10147

1

There are 1 answers

2
Gabe On

A clue:

It looks like the deeplinks plugin is using window.location.href to detect the query parameter.

Since I am using cordova-plugin-ionic-webview the href is always the alias used for localhost of the Ionic engine serving the App contents, so the query parameters are never found.

Deeplinks plugin code:

https://github.com/ionic-team/ionic-plugin-deeplinks/blob/master/src/browser/DeeplinkProxy.js#L40

function locationToData(l) {
  return {
    url: l.href,
    path: l.pathname,
    host: l.hostname,
    fragment: l.hash,
        scheme: parseSchemeFromUrl(l.href),
        queryString: parseQueryStringFromUrl(l.href)
  }
}

onDeepLink: function(callback) {
  // Try the first deeplink route
  setTimeout(function() {
    callback && callback(locationToData(window.location), {
      keepCallback: true
    });
  })
  // ...
}

This is the problem, not sure on the solution yet though.