1)ulrshemaURL插件在IOS测试没有问题,使用定时器就可以。
2)在android测试有BUG,网上有很多答案都不靠谱。
ANDROID的解决方案
1)第一步:需要调整android的activity的单任务模式,放置当前APP被调用者替换掉。有很多解决方案但是修改config.xml最简单。
Config.xml
......
<preference name="AllowInlineMediaPlayback" value="true"/>
<!-- 屏幕方向 default, all, portrait, landscape -->
<preference name="Orientation" value="portrait"/>
<preference name="SplashScreen" value="screen"/>
<preference name="SplashScreenDelay" value="10000"/>
<plugin name="cordova-plugin-whitelist" spec="1"/>
<!-- 修改对URLSchema的支持 -->
<preference name="AndroidLaunchMode" value="singleTask"/>
</widget>
2)第一次接收的问题,修改cordova-plugin-customurlscheme\www\android的源代码,采用定时器,配合token进行判断。
(function () {
"use strict";
var remainingAttempts = 10;
function waitForAndCallHandlerFunction(url) {
if (typeof window.handleOpenURL === "function") {
// Clear the intent when we have a handler
openUrl(url,0);
cordova.exec(
null,
null,
"LaunchMyApp",
"clearIntent",
[]);
} else if (remainingAttempts-- > 0) {
//alert("remainingAttempts");
setTimeout(function(){waitForAndCallHandlerFunction(url);}, 10);
}
}
function openUrl(url,i)
{
window.handleOpenURL(url);
if(i<10)
{
setTimeout(function(){
openUrl(url,i+1);
},10);
}
}
function triggerOpenURL() {
cordova.exec(
waitForAndCallHandlerFunction,
null,
"LaunchMyApp",
"checkIntent",
[]);
}
document.addEventListener("deviceready", triggerOpenURL, false);
var launchmyapp = {
getLastIntent: function(success, failure) {
cordova.exec(
success,
failure,
"LaunchMyApp",
"getLastIntent",
[]);
}
}
module.exports = launchmyapp;
});
编写协议应该采用EncodeURI的方式(因为IOS必须采用此方式),外部截取token避免这里的定时器对此重发。
getOpenUrl:function(url,func)
{
var index=url.indexOf("?");
var token=url.substr(index+"?".length);//uuid
var urldata= url.substr(0,index);
if(uuid!=token)//避免多次重发
{
urldata=decodeURI(urldata);//兼容IOS支持采用encode的解析。
func("Y",urldata);
uuid=token;
}
else
{
func("N","");
}
}