So far the above contents have installed necessary libraries, register our account to the Google Cloud Console, and complete our client side. In this section, we need to create a simple application server with PHP language.
A. Create Table in your database
This application server receives the registration id from the application, stores it in the database and sends the message to the application using GCM connection server. To store the registration id, create a simple MySQL table using following query, as the following diagram.
B. Create a register.php file
When android application runs for first time, it registers the application to the GCM using sender ID (i.e. project ID mentioned above) and gets the registration Id. The application then sends the registration ID to the application server to store in the database. Here is the PHP file register.php which does the above mentioned task i.e. it receives the registration id and stores it in the database. If you want to see the most complete source code, you can refer to sample code about the GCM server.
D. Create a index.php file
If you want to send the message to the application then you write a message and fetch all the registration ids from the database. You then send the registration ids along with the message to the GCM server. The GCM servers gives the response back to the application server. index.php file does the above mentioned task i.e. it sends the message to the application and echo back the response from the GCM server. By the way, you should replace the API Key you appled, as the following diagram. If you want to see the most complete source code, you can refer to sample code about the GCM server.
If you want to send the message to the application then you write a message and fetch all the registration ids from the database. You then send the registration ids along with the message to the GCM server. The GCM servers gives the response back to the application server. index.php file does the above mentioned task i.e. it sends the message to the application and echo back the response from the GCM server. By the way, you should replace the API Key you appled, as the following diagram. If you want to see the most complete source code, you can refer to sample code about the GCM server.
2.Test result
First, we launched an emulator from Android Virtual Device Manager, and pressed the button named “Register Device” in Figure 9; meanwhile, the system will show a Toast message.
After registration to the server side, your registration id from the client side will be record in the database, as the following diagram.
To open index.php file through your web browser delivers a message, as the following diagram.
Finally, you can see the message from the server side in your emulator, as the following diagram.
3.References
A. GCM: https://developer.android.com/google/gcm/index.html
B. Server side: http://gitlab.hq.hiiir/xavier_yin/my_gcm_server_demo.git
沒有留言:
張貼留言