Red-Folder / Cordova-Plugin-BackgroundService

BackroundService plugin for use with Cordova (PhoneGap)
144 stars 85 forks source link

Using background service to make an existing plugin run in the background #2

Closed rlad closed 11 years ago

rlad commented 11 years ago

Hi, thanks for the fantastic contribution.

I'm trying to get background SMS receive to work such that my app will be started if an SMS comes in, will get the SMS and be able to decide whether or not to post a notification (it only cares about some SMS messages).

This plugin seems to work fine when an app is running: https://github.com/Pyo25/Phonegap-SMS-reception-plugin

But I need it to work in the background too, and thought of using your plugin to do it.

Since many people probably would like to get various plugins to run in the background, I was wondering if you could provide some tips or procedures for accomplishing this.

Red-Folder commented 11 years ago

I probably wouldn't use my background service or a any plugin for this.

You can define a java class that would monitor for RECIEVE_SMS broadcast. That class can monitor for specific SMS message and on receipt raise a notification (which would start your app). Your java class will be registered via the AndroidManifest.xml to constantly listen for SMS and act on it.

If you look at this tutorial it shows you how to create a broadcast receiver for the SMS -> http://webtutsdepot.com/2011/09/28/android-sdk-tutorial-how-to-receive-text-messages/

For your purposes, you will want to amend the onReceive of the TextMessageReceiver to check if it is the SMS you expect and raise a notification.

Hope this helps.

rlad commented 11 years ago

Thanks for the reply.

In this case, a plugin would be necessary because the phonegap app has to decide whether or not the SMS message is of interest based on its content. And then it has to store (in localstorage) and display it to the user.

The SMS reception plugin I linked above already does do the receive, but not as an independently running service, which is what is needed so that the app can be started whenever an SMS of interest is received by the phone.

By the way, there is no obvious way to contact you (for example, to inquire about consulting) other than by posting issues here. I looked for an email but didn't find one.

Red-Folder commented 11 years ago

You can contact me on mark bryan taylor at gmail dot com

No I don't think you need an independent running service. I believe (although this is untested) that if you hook up to the Android Broadcast mechanism for RECEIVE_SMS then Android will automatically run your code when the SMS is received. Might be worth some research on google.

To pass this data to your app;

You would create a notification (google with have lots of examples). Against that notification you can define which app to start when the user clicks on it. You can also specify any data you want to pass to the app (in this case the SMS message). Within your app you would need to do some work to read that data.

rlad commented 11 years ago

That plugin linked above does hook up to the Android Broadcast mechanism using RECEIVE_SMS, and the app only sees the SMS if it's running (not paused). Perhaps it needs to be configured in some other way in order to have Android start it when an SMS comes in?

The solution you suggested of creating a notification and then passing the SMS to the phonegap app when the user starts it by tapping the notification would work if the app could use every SMS which came in, however it has to deal with a couple of other situations:

Because of these issues, I thought that a background service would work because it would:

On the other hand, I'm not sure if Android would allow the phonegap app to start under these circumstances, or if the app could start and just determine whether a notification should be created, without being shown on the screen.

Red-Folder commented 11 years ago

I wouldn't pass the SMS to the phonegap app to determine whether it's of interest. This would involve the phonegap having to open and display just to check if the SMS is of interest. Better to have the java code decide if of interest.

The java code would need access to the list of open messages to monitor for. On receipt of a matching message it would save the message to database and create a notification. When the app opens it can look in the database for any new messages. Because a databases is in use then the java can log all messages until such time as the user decides to review them.

You might need a plugin to allow interaction - passing of the list from app to java code and messages from the db to app. I'm not sure if the the app and java code can share a db directly.

It's an interesting problem - I'm afraid I don't have a conclusive answer for you.

askeypuah32 commented 11 years ago

Hi,

May i know this phonegap background plugin able to get current location running at background?

Thanks,

Puah.