简体   繁体   English

处理基于meteorjs(myapp://)构建的cordova应用程序中的深层链接

[英]Handle deep linking in cordova app built on meteorjs (myapp://)

I'm building a meteorjs app and deploying it as a native (cordova) app for Android and iOS. 我正在构建一个meteorjs应用程序,并将其部署为适用于Android和iOS的本机(cordova)应用程序。

I need to deep link to my app, so I can launch it by following a link on a website. 我需要深层链接到我的应用程序,因此可以通过跟随网站上的链接来启动它。

I've implemented URL Scheme using the cordova plugin by Eddy Verbruggen ( https://github.com/EddyVerbruggen/Custom-URL-scheme ) and I managed to get my app launched by following a link of the "myapp://" format. 我已经使用Eddy Verbruggen( https://github.com/EddyVerbruggen/Custom-URL-scheme )的cordova插件实现了URL Scheme,并且通过点击“ myapp://”链接成功启动了我的应用格式。

The problem is that, even though the app is getting successfully launched, the handleOpenUrl hook is not getting triggered. 问题是,即使该应用程序已成功启动,也不会触发handleOpenUrl挂钩。

Meteor.startup(function() {
    handleOpenURL = function handleOpenURL(url) {
        console.log("received url: " + url);
        // parse url and proceed accordingly
    }
});

Nothing gets logged. 什么都没有记录。 So I have no way of passing any parameters to my app, or even detect that the app was launched via a link that follows the 'myapp://' URL scheme. 因此,我无法将任何参数传递给我的应用程序,甚至无法检测到该应用程序是通过遵循“ myapp://” URL方案的链接启动的。

Also, I noticed that even though my app is already open, if I switch to the browser and click on a special "myapp://" link, instead of switching to my (already open) app, a new instance of the app is getting launched. 另外,我注意到即使我的应用程序已经打开,但如果我切换到浏览器并单击特殊的“ myapp://”链接,而不是切换到我的(已经打开的)应用程序,该应用程序的新实例就是启动。

[edit] Turned out the two problems were related. 原来,这两个问题是相关的。 Once I prevented a new instance of the app from being initialized, the handleOpenUrl function was triggered successfully. 一旦我阻止了该应用程序的新实例初始化,handleOpenUrl函数就会成功触发。

I'm using the Android emulator for all my tests. 我正在使用Android模拟器进行所有测试。 I haven't had the chance to test on iOS yet. 我还没有机会在iOS上进行测试。

[edit] iOS didn't give this problem at all. [edit] iOS根本没有解决这个问题。 It was only an Android problem. 这只是一个Android问题。

If anyone out there has any experience on implementing custom URL scheme in meteorjs, your feedback is much appreciated! 如果有任何人有在meteorjs中实现自定义URL方案的经验,非常感谢您的反馈!

I managed to fix the issue I was facing. 我设法解决了我面临的问题。 Here is the solution, for anybody else who is facing similar problems. 对于其他面临类似问题的人,这是解决方案。

First of all, it was only occurring on Android. 首先,它仅在Android上发生。 iOS had no problem whatsoever to launch the app and trigger the handleOpenUrl function. iOS完全可以启动应用程序并触发handleOpenUrl函数。

The two issues that I was facing on Android turned out that there were related to each other. 我在Android上面临的两个问题原来是相互关联的。 The fact that a new instance of the app was triggered every time, prevented the handleOpenUrl function from being triggered. 每次都触发应用程序的新实例这一事实阻止了handleOpenUrl函数被触发。

Both problems were fixed by adding a single line of code on the mobile-config.js file: 通过在mobile-config.js文件上添加一行代码,解决了两个问题:

App.setPreference("AndroidLaunchMode", "singleTask");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM