Android OnMSArrive cordova sms插件执行多次

Android OnMSArrive cordova sms插件执行多次,android,angularjs,cordova,ionic-framework,cordova-plugins,Android,Angularjs,Cordova,Ionic Framework,Cordova Plugins,我正在开发一个应用程序,当用户收到短信时,它可以读取短信内容 所以我使用爱奥尼亚和科尔多瓦短信插件来阅读短信内容。但当用户收到短信并触发插件提供的onSMSArrive事件时,它确实起作用,可以读取短信内容 问题是它执行(阅读短信)不止一次,确切地说是三次 我将此代码作为服务放在ionic中 app.factory('$smsarrive', [function() { return { periksa:function() { if (SMS)

我正在开发一个应用程序,当用户收到短信时,它可以读取短信内容

所以我使用爱奥尼亚和科尔多瓦短信插件来阅读短信内容。但当用户收到短信并触发插件提供的onSMSArrive事件时,它确实起作用,可以读取短信内容

问题是它执行(阅读短信)不止一次,确切地说是三次

我将此代码作为服务放在ionic中

app.factory('$smsarrive', [function() {
    return {
        periksa:function() {

            if (SMS) SMS.enableIntercept(true, function() {
                    console.log("some debug hint here");
                }, function(){
                    console.log("some debug hint here");
                });

            if(SMS) SMS.startWatch(function() {
                //update('watching', 'watching started');
                    console.log("some debug hint here");
                }, function(){
                   //updateStatus('failed to start watching');
                   console.log("some debug hint here");
                });

            document.addEventListener('onSMSArrive', function(e) {
                var sms = e.data;
                var isiSms = sms.body;

                if (isiSms.match(/FC0019229/g)!=null) {
                    if (isiSms.match(/Berhasil/g)!=null) {
                        console.log("Isi pulsa Berhasil");
                    } else if (isiSms.match(/Gagal/g)) {
                        console.log("Isi pulsa Gagal");
                    } else {
                        console.log(isiSms);
                    }
                } else {
                    console.log("some hint here");
                }
                console.log("ASLI : "+isiSms);                       
            });
        }

    }
}])
并在视图的控制器运行时执行该服务

$scope.$on('$ionicView.enter', function() {
    $smsarrive.periksa();
})
有什么建议吗?同时也为糟糕的英语感到抱歉


我使用

根据我在代码示例中的理解,每次输入视图时,您都在执行函数“periksa”(这就是触发“$ionicView.enter”事件的原因)。然后,每次执行“periksa”(每次输入视图)时,您都要为SMS消息创建一个EventListener

因此,如果您输入了三个视图,则会有三个侦听器为SMS OnMSArrive的到来而触发

因此,我认为您应该只在启动应用程序时(在app.js中,inside.run中,当$ionicPlatform.ready()时)开始观看并添加一次EventListener

然后,“periksa”函数应该只接收作为参数的数据,用于处理和处理结果


让我知道这是否有用,我也在研究这个框架,我的应用程序中有这个插件。

请提供一个指向您正在使用的特定插件的链接。@MikeM。谢谢你的评论,我编辑了我的问题以提供插件url。是的,那个插件不能正确处理传入的消息。这是发生在每一条消息上,还是只是长消息?你测试的信息有多长时间?@MikeM。我收到的每一条信息。即使有三两个字母。我找不到任何其他sms插件提供处理传入消息的功能。有什么建议吗?再次感谢。嗯,我可不想那么做。我不使用Cordova,但我以前看过该插件的Java源代码,我知道它错误地处理了多部分消息,这导致它为每个部分触发一次。收到多部分消息时会发生什么情况?也就是说,在基本ASCII中,长度超过160个字符。它会开六次吗?
.run(function($ionicPlatform, $smsarrive) {
   $ionicPlatform.ready(function() {
   if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
     cordova.plugins.Keyboard.disableScroll(true);

   }
   if (window.StatusBar) {
    // org.apache.cordova.statusbar required
    StatusBar.styleDefault();
   }

   if(( /(ipad|iphone|ipod|android)/i.test(navigator.userAgent) )) {

     if (! SMS ) { alert( 'SMS plugin not ready' ); return; }

     SMS.startWatch(function(){
       console.log('Watching');
     }, function(){
       console.log('Not Watching');
     });

     document.addEventListener('onSMSArrive', function(e){
            var data = e.data;
            $smsarrive.periksa(data);
     });

   } else {
     alert('need run on mobile device for full functionalities.');
   }
});