问题
I have a web service that stores locations of a mobile user in the DB periodically. I now want to try sending push notifications from the web service to the Android app. I understand that this can be achieved using GCM for Android.
However, I am not sure how I can run a process that continuously monitors the DB for a condition. Can somebody suggest how I can achieve this?
Will creating a thread to do the monitoring task in the constructor of the web service help to maintain a constant monitor?
P.S: At the moment I am just starting a thread which will check the DB for the condition I need when the app calls the store method and then sends POST request to GCM server to push notifications if the condition is satisfied.
回答1:
Steps to accomplish this task -
- Create a
servletand register it in the deployment descriptor - Override the
initmethod of the servlet - Create a thread and start it in the
initmethod. Use awhileloop to execute infinitely to do the monitoring task - Most important - force the servlet initialization during server startup by adding the following line to the deployment descriptor. Info at this link - http://jersey.576304.n2.nabble.com/Initialization-at-startup-as-opposed-to-first-request-td5164440.html
来源:https://stackoverflow.com/questions/26735941/push-notifications-from-a-web-service