Red-Folder / bgs-core

Core code for the Cordova Background Service
Other
236 stars 104 forks source link

This plugin can be used in Ionic 2/3? #85

Open Juanancon opened 7 years ago

Juanancon commented 7 years ago

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

Red-Folder commented 7 years ago

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

andrewvmail commented 7 years ago

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

robert719 commented 6 years ago

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

andrewvmail commented 6 years ago

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

gauravmehla commented 6 years ago

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

kpwa commented 6 years ago

how can i include this in my home.ts?

gauravmehla commented 6 years ago

@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.

kpwa commented 6 years ago

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

Dudix93 commented 6 years ago

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

kashifsulaiman commented 5 years ago

@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?