bgs-core icon indicating copy to clipboard operation
bgs-core copied to clipboard

This plugin can be used in Ionic 2/3?

Open Juanancon opened this issue 7 years ago • 10 comments

I've just discovered this plugin and it's great, but I'm not sure if there is any way to implement this code to an Ionic project or should I look for something else. I'm relatively new using Ionic and I have no idea if I can implement a Cordova project on Ionic so easily

Juanancon avatar Sep 01 '17 10:09 Juanancon

I'm afraid I don't know. I've never used Ionic

Red-Folder avatar Sep 18 '17 20:09 Red-Folder

i just tried it and it works fine https://github.com/Red-Folder/bgs-sample.git used the sample

andrewvmail avatar Dec 03 '17 04:12 andrewvmail

Hi, Andrew Did you use this plugin with Ionic 2/3 ? Then could you send me sample project for it?

robert719 avatar Jan 11 '18 03:01 robert719

yeah ionic 3 but sorry i dont have a sample project i just played around with it and didnt end up using this but i know it works because i just followed the turotials and hooked in console to run the js + logcat and verify its doing stuff the sample project should do it https://github.com/Red-Folder/bgs-sample.git

andrewvmail avatar Jan 12 '18 10:01 andrewvmail

Yes. We can use this plugin with ionic 2/3. I am using it and will post a blog soon. Keep checking https://medium.com/dev-blogs

gauravmehla avatar Feb 09 '18 10:02 gauravmehla

how can i include this in my home.ts?

kpwa avatar Feb 24 '18 23:02 kpwa

@kpwa add this in your index.html file

<script type="text/javascript">
        myService = "";
        serviceRunning = "";

       document.addEventListener('deviceready', function() {
          var serviceName = 'com.red_folder.phonegap.plugin.backgroundservice.sample.MyService';
          var factory = cordova.require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService');
          myService = factory.create(serviceName);

          
       }, true);

       function getStatus() {
         return new Promise( function( resolve, reject ){
              function displayResult(data) {
                console.log( data );
               resolve(data.ServiceRunning);
              
            }
            myService.getStatus(function(r){displayResult(r)}, function(e){displayError(e)});
         });
       }

       

       function displayError(data) {
          alert("We have an error");
       }

       function updateHandler(data) {
        console.log( data );
           if (data.LatestResult != null) {
              try {
                 console.log(data.LatestResult.Message );
              } catch (err) {
              }
           }
        }

        function go() {
           myService.getStatus(function(r){startService(r)}, function(e){displayError(e)});
        };

        function startService(data) {
          if (data.ServiceRunning) {
             enableTimer(data);
          } else {
             myService.startService(function(r){enableTimer(r)}, function(e){displayError(e)});
          }
       }

       function enableTimer(data) {
          if (data.TimerEnabled) {
             registerForUpdates(data);
          } else {
             myService.enableTimer(1000, function(r){registerForUpdates(r)}, function(e){displayError(e)});
          }
       }

       function registerForUpdates(data) {
        if (!data.RegisteredForUpdates) {
           myService.registerForUpdates(function(r){updateHandler(r)}, function(e){ console.log(e) });
        }
     }

      function stopService() {
            myService.stopService(  function(r){ console.log('Service Stopped'); },
                                    function(e){ alert('Error while stopping the service.')});
        }
     </script>

And in your home.ts use these methods with window. i.e window.go(); to start the service.

gauravmehla avatar Feb 25 '18 07:02 gauravmehla

that did the trick. Thanks @gauravmehla you just saved a dev

kpwa avatar Feb 25 '18 12:02 kpwa

But how can i use it to trigger the method typed in typescript which for instance, connects with an api?

Dudix93 avatar Jun 22 '18 20:06 Dudix93

@kpwa add this in your index.html file

<script type="text/javascript">
        myService = "";
        serviceRunning = "";

       document.addEventListener('deviceready', function() {
          var serviceName = 'com.red_folder.phonegap.plugin.backgroundservice.sample.MyService';
          var factory = cordova.require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService');
          myService = factory.create(serviceName);

          
       }, true);

       function getStatus() {
         return new Promise( function( resolve, reject ){
              function displayResult(data) {
                console.log( data );
               resolve(data.ServiceRunning);
              
            }
            myService.getStatus(function(r){displayResult(r)}, function(e){displayError(e)});
         });
       }

       

       function displayError(data) {
          alert("We have an error");
       }

       function updateHandler(data) {
        console.log( data );
           if (data.LatestResult != null) {
              try {
                 console.log(data.LatestResult.Message );
              } catch (err) {
              }
           }
        }

        function go() {
           myService.getStatus(function(r){startService(r)}, function(e){displayError(e)});
        };

        function startService(data) {
          if (data.ServiceRunning) {
             enableTimer(data);
          } else {
             myService.startService(function(r){enableTimer(r)}, function(e){displayError(e)});
          }
       }

       function enableTimer(data) {
          if (data.TimerEnabled) {
             registerForUpdates(data);
          } else {
             myService.enableTimer(1000, function(r){registerForUpdates(r)}, function(e){displayError(e)});
          }
       }

       function registerForUpdates(data) {
        if (!data.RegisteredForUpdates) {
           myService.registerForUpdates(function(r){updateHandler(r)}, function(e){ console.log(e) });
        }
     }

      function stopService() {
            myService.stopService(  function(r){ console.log('Service Stopped'); },
                                    function(e){ alert('Error while stopping the service.')});
        }
     </script>

And in your home.ts use these methods with window. i.e window.go(); to start the service.

Where to you put MyService.java?

kashifsulaiman avatar Nov 21 '19 12:11 kashifsulaiman