2

Consider the apps that large blogs have (which work pretty much like an RSS feed but for your phone). Whenever the blog publishes a new post the app creates a notification on the user's phone about the new post.

Here's are some things I would like to understand about those:

  1. Is that notification being pulled by the app on my phone or being pushed to it by the blog?
  2. How come this works even if I have not launched the app?
  3. What classes and services of the Android platform are used to create such apps?

Thanks a lot.

Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78

4 Answers4

3

You could easily do something like this by:

  1. Have you app set an alarm using AlarmManager
  2. Have the receiver for the alarm start a Service that checks for updates
  3. Display the notification to the user using the NotificationManager

That is a polling solution, which should be fine and probably better in most circumstances like you described.

However, if you want a push solution, you can look into Android C2DM which allows your server to send push notifications to a registered Android device.

Edit

How do apps run 'without being launched'?

Well there's a few ways. One is that any app can register for different device events; one of those is a boot-complete event.

The second way is using the AlarmManager. If an app has set an alarm then the user navigates away from the app and then the OS decides to close the app, the alarm can still be registered to run at a certain time. When this alarm is triggered will run whatever code the app developer likes, including starting a Service. Of course doing this without notifying the user can be an issue, especially if the service drains battery, etc.

you786
  • 3,659
  • 5
  • 48
  • 74
  • 1
    I still don't get point 2. How can the app set a Service that checks for updates if I haven't even launched it? That is, I turn on my phone, and even without launching the app I'll get those notifications. Or someone else and not my app will set that service? – Daniel Scocco Jun 20 '12 at 22:48
  • The app can register for different events like phone boot up, etc. Also if an app sets an alarm, the app can be completely closed when the alarm goes off, and the OS will launch whatever part of the app was specified in the alarm. – you786 Jun 20 '12 at 23:04
1

Of course, there are a lot of ways it can be done, but in the general case, I would imagine:

  1. It is pulled by the phone
  2. Even if you do not see the app, it can have a service running in the background
  3. That's really up to what the app does, any specific aspect you want to know about ?
Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • Regarding 2, who starts that service once I turn the phone on, cause even without launching the app I'll still get the notifications. Thanks. – Daniel Scocco Jun 20 '12 at 22:49
  • Most of the time, you would want your app to receive the ACTION_BOOT_COMPLETED broadcast signal to start the service – Matthieu Jun 20 '12 at 22:57
1

One way:

  1. apps can launch a Sercvice via Broadcast after boot. See here: How to Autostart an Android Application?
  2. The app starts an Service on startup, a Service is like an Activity, except without UI and it runs in Background
  3. The Service looks for new Posts from time to time, implemented for example as Runnable in a ScheduledThreadPoolExecutor
  4. The Runnable finds a new Post and makes a Notification

Note: This is also a poll solution. instead of ScheduledThreadPoolExecutor you could use a Handler that always calls itself or just a Thread that sleeps and wakes up from time to time. IMHO the ScheduledThreadPoolExecutor is the 'nice' way.

Community
  • 1
  • 1
trichner
  • 840
  • 1
  • 11
  • 22
  • Thanks, I didn't know aout the autostart thing. Makes sense. – Daniel Scocco Jun 20 '12 at 22:50
  • I don't think there is anything like registering for AutoStart... check this to automatically start your service: http://stackoverflow.com/questions/1056570/how-to-autostart-an-android-application – Matthieu Jun 20 '12 at 23:00
  • Well, as far as I can tell, facebook app does launch on startup, but I honestly don't know how^^ – trichner Jun 20 '12 at 23:04
  • There is definitely registering for device turn on (not called AutoStart but close enough) – you786 Jun 20 '12 at 23:04
  • Ok, post http://stackoverflow.com/questions/1056570/how-to-autostart-an-android-application describes my point 1 ;) I didn't know how to implement that... so it's obviously not called 'register' (edit: edited) – trichner Jun 20 '12 at 23:09
1

Is that notification being pulled by the app on my phone or being pushed to it by the blog?

It could be either.

How come this works even if I have not launched the app?

Well, on Android 3.1+, you will need to have run the app at least once for any of that stuff to work.

Beyond that, they are using stuff like AlarmManager (for pull) or C2DM (for push), neither of which require you to have "launched the app", any more than you need to keep the Gmail activity open to be able to get new email messages.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491