How can i get data to be pushed from the database to the client, like a messenger service?

喜欢而已 提交于 2019-12-11 22:13:25

问题


I am trying to code a messenger service that will store sent messages in a server and then push the message to all devices connected to the server.

The messenger will be used in a classroom environment and the messages will never be sent to one individual device. At the moment a message can be sent and stored in the database but the app needs to be refreshed for all the messages to be displayed. A simple way would be to continuously run the stored procedure but that would require extra bandwidth and i'm sure there is a better way to do it.

I am currently coding in Android Studio and I'm using MySQL as my database and PHP for my stored procedures. I am very new at Java and this type of coding so any help will be appreciated.

Thanks


回答1:


Yes, what you are suggesting is named "polling" => check if the server has some updates each 10sec or something like that. You're right, it's evil (drains the battery, use a lot of bad=ndwidth, etc.)

What you need is a "push" service. If you are working with Android, the easiest way of doing it is to use GCM (Google Cloud Messaging) : http://developer.android.com/google/gcm/index.html

This little guy will provide you a way of notifying all the devices that theure have been some changes on the server.

It's pretty well done (if it can't reach the devices, it will try again with an exponential time delay, etc.).




回答2:


You can use google cloud messaging service to send notification in device so please register your application on google service all step are cover below link. Open below link and follow step

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/



来源:https://stackoverflow.com/questions/25123025/how-can-i-get-data-to-be-pushed-from-the-database-to-the-client-like-a-messenge

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