I am a newbie in android and push notifications and i have built a PHP page to send such push messages. as long as the app is in foreground - i can detect and show an alert dialog. when onReceive() is called. when i press the "Back" button , the app (i guess) closes and the push notifications won't wake it up. is it possible to do so ? (Case study from a different platfom - facebook application not running but push can activate it)
Asked
Active
Viewed 1,594 times
3 Answers
1
You can use Services from Android.
public class MyService extends Service {
private Timer timer;
@Override
public void onCreate() {
super.onCreate();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
doSomethingInBackground
}, 0, 1000);
}
}
The Android Dev Guide has a lot of samples and tutorials about services

Stephan
- 15,704
- 7
- 48
- 63
-
so i should move my methods for starting the receivers to the service class? thanx for the reply!!! – Yotamix Jan 08 '12 at 13:07
-
You can also start an Activity from your Service or display a notification at the status bar of the phone. – Stephan Jan 08 '12 at 13:15
-
when a notification comes , the status bar shows it has arrived , but i what my app to show it in a dialog with Yes/No buttons. which means i need my app to come to the foreground to do so. – Yotamix Jan 08 '12 at 13:18
-
To get the app(the Activity) in the foreground have a look at this link: [link](http://stackoverflow.com/a/3607934/1031556) – Stephan Jan 08 '12 at 13:26
-
i have started the registration for notification in the service , which was started in the activity. now if i try to get an alert out when the message arrives - it crashes : either babToken - no window in the service , or the activity doesnt exists and it is null. – Yotamix Jan 08 '12 at 13:55
0
Android can close your app anytime it gets hibernated if it needs the memory. You can probably do what you're asking with a service.
-
i wish not to use mqtt , i implemented c2dm as shown with a registration reciever and message reciever . i can see in the emulator that the notification arrives on the top bar , but pressing it shows an empty screen and when the message gets in , the app isn't being activated. i have inserted logs to see the calls of methods and i see it is being closed every time i press the "Back" button in the emulator. is their a way to keep it alive so when a message gets in , i can see my app/dialog box ? – Yotamix Jan 08 '12 at 13:06