Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Juanancon opened this issue Sep 1, 2017 · 10 comments
Open

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

Juanancon opened this issue Sep 1, 2017 · 10 comments

Comments

@Juanancon
Copy link

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 Juanancon reopened this Sep 1, 2017
@Red-Folder
Copy link
Owner

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

@andrewvmail
Copy link

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

@robert719
Copy link

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

@andrewvmail
Copy link

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
Copy link

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
Copy link

kpwa commented Feb 24, 2018

how can i include this in my home.ts?

@gauravmehla
Copy link

@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
Copy link

kpwa commented Feb 25, 2018

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

@Dudix93
Copy link

Dudix93 commented Jun 22, 2018

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

@kashifsulaiman
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants