cordova开源组件urlschemaURL的在第一次启动的无法获得消息的BUG

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


结束。



    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值