In-app notification triggered from a background service android

712 views Asked by At

Lately I am developing some new features of the app. My app is about displaying different events that are happening in a specific time and in a specific place. Now I want for the user to be able to save a event like his favorite, which means that he potentially wants to attend that particular event. When a user mark an event as a favorite one, the time of the event and other details like place, name, description will be store on the device. Now the part that I am having some difficulties is the notification part.

What I want to do is that when the time is 1 hour before the beginning of the event a notification must be triggered in order to tell the user that the event will start in an hour. If I will say its an analogy with the agenda notifications.

Now the problem is to the right way of implementing it. I know how to use Notification in Android and also I know how to use background services . I have followed a lot of tutorials like : http://www.tutorialspoint.com/android/android_services.html, but I don't know which is the right way of implementing it.

One way is to store the data in a database, and run a background service to check periodically if the time of the phone is the right to trigger a notification for an event.But this would say that the service would run indefinitely in the background.

I also thought about using some store procedures in the database that check the values that are store over there and when the time comes, it triggers the service that triggers the notification.

I don't know if I am following the right way and I need to clarify these before I proceed.

Is there any tutorial to help me or any other idea that helps me achieve the same thing.

1

There are 1 answers

2
CommonsWare On BEST ANSWER

One way is to store the data in a database, and run a background service to check periodically if the time of the phone is the right to trigger a notification for an event.But this would say that the service would run indefinitely in the background.

This is rather wasteful. Is is the programming equivalent of the children in the back seat of the car chanting "are we there yet? are we there yet?".

I also thought about using some store procedures in the database that check the values that are store over there and when the time comes, it triggers the service that triggers the notification.

This is not supported in Android.

or any other idea that helps me achieve the same thing

Use AlarmManager to tell Android to give you control one hour before your event. The PendingIntent that you use with AlarmManager can trigger a BroadcastReceiver in your app that can raise the Notification.