首页 > 解决方案 > Cordova 深层链接 - 未从通用链接中检测到查询参数

问题描述

我正在尝试使用ionic-plugin-deeplinks插件将通用链接添加到 Cordova 应用程序。

根据这个问题,查询参数应该开箱即用。

对我来说,通用链接可以正常工作,但带有查询参数的链接除外

例如。https://my-site.com/?olddeeplinking=resetpassword&token=123


当我单击电子邮件中的链接时,queryString 字段始终为空字符串

在此处输入图像描述

我是否遗漏了什么,我是否需要启用插件来检测查询参数?

这是我正在使用的代码:

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);
};

更新:看起来在 Android 上收到的意图总是 /user/login 即使通用链接没有它。可能是什么原因造成的?

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

标签: ioscordovaionic-frameworkios-universal-linksionic-webview

解决方案


线索:

看起来deeplinks插件正在window.location.href用于检测查询参数。

由于我使用的是cordova-plugin-ionic-webviewhref它始终是用于为应用程序内容提供服务的离子引擎的本地主机的别名,因此永远找不到查询参数。

深度链接插件代码:

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
    });
  })
  // ...
}

这就是问题所在,但尚不确定解决方案。


推荐阅读