In-app notification triggered from a background service android

浪子不回头ぞ 提交于 2019-12-25 03:16:31

问题


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:


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.



来源:https://stackoverflow.com/questions/27176797/in-app-notification-triggered-from-a-background-service-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!