Open Juanancon opened 7 years ago
I'm afraid I don't know. I've never used Ionic
i just tried it and it works fine https://github.com/Red-Folder/bgs-sample.git used the sample
Hi, Andrew Did you use this plugin with Ionic 2/3 ? Then could you send me sample project for it?
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
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
how can i include this in my home.ts?
@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.
that did the trick. Thanks @gauravmehla you just saved a dev
But how can i use it to trigger the method typed in typescript which for instance, connects with an api?
@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 withwindow
. i.ewindow.go();
to start the service.
Where to you put MyService.java?
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