I am pretty new in Ionic 2 and I am working on an application which have to send SMS depending on the current hour.
To achieve this, I guess that I have to use a service worker and this is my issue.
In traditional component I would do something like this :
import { SMS } from '@ionic-native/sms';
constructor(private sms: SMS) { }
...
setInterval(function(){
if(isTimeToSendSMS()) { // call function that compare time
this.sms.send('416123456', 'Hello world!');
}
}, 600000); // check every 10 minutes
Unfortunally I’m not able (understand I don’t know how) to do this in a service worker and I’m asking myself some questions :
Can we use native component in a service worker ? (How ?)
Is it the right way to achieve my project ? If not, could you guide me to the right direction ?
Can we use native component in a service worker ? (How ?)
A service work is not meant to be used for this.
It has no context to the rest of your app and is meant to act as a proxy between your app and the network.
Is it the right way to achieve my project ? If not, could you guide me to the right direction ?
Overall I suggest people do attempt these kind of features. As a user, I would be a fan of an app trying to send SMS every time the app is closed or depending on the hour.
A better option could be the local notification plugin.
Since it integrates better with the OS and device and does not force the user out of the app.
I already use this plugin and as you noticed, It can’t be a solution cause the user could kill the process anytime by definitly closing the app.
A better alternative could be this plugin http://ionicframework.com/docs/native/background-fetch/ but I’m not sure if it starts app or only wakeup a background mode app ? Anyway this is only available for IOS and my app is targeting Android for now.
@mhartington, I understand that you could disagree with the idea of application do something like sending SMS depending on the hour but I intentionnally simplified the context.
Actually, the application could be something like this (not sure, for now I’m just experimenting with Ionic) :
“My mom told me to come back home at 6:00 PM, It’s 6:30 PM, compare my GPS position and if i’m not near home send her my position by SMS”
So if I understand, the only way to do it is to write a native service (in JAVA for example) and run it with my app ? :’(