In the previous articleArticleWe introduced how to create a Google API project and get your Project ID and server key..In this article, we need to push messages using these two key values. This example is rewritten based on the official examples provided by Google. If you need to download the Google example, you can download Google cloud messaging for Android library from the extras of Android SDK manager, the example is in Android-SDK-Windows \ extras \ google \ GCM.
In AndroidProgramAddedCodeAnd then enable the broadcast service..
Mainactivity. Java
@ Override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); checknotnull (sender_id, "sender_id"); // sendid is the Project ID gcmregistrar we obtained in the previous chapter when creating a Google API project. checkdevice (this); gcmregistrar. checkmanifest (this); setcontentview (R. layout. main); mdisplay = (textview) findviewbyid (R. id. display); registerreceiver (mhandlemessagereceiver, new intentfilter (display_message_action); string regid = gcmregistrar. getregistrationid (this); // The device ID if (regid. equals ("") {// if it is null, You need to register gcmregistrar with your API project. register (this, sender_id); string rid = gcmregistrar. getregistrationid (this); log. I (TAG, "registering device (regid =" + rid + ")"); gcmregistrar. setregisteredonserver (this, true);} else {If (gcmregistrar. isregisteredonserver (this) {mdisplay. append (regid + "\ n"); log. I (TAG, "registering device (regid =" + regid + ")");} else {gcmregistrar. setregisteredonserver (this, true );}}}
Then, read the information after receiving the message.
Gcmintentservice. Java
@ Override protected void onmessage (context, intent) {log. I (TAG, "received message"); string message = ""; try {string action = intent. getaction (); If (action. equals ("com. google. android. c2dm. intent. receive ") {message = intent. getstringextra ("MSG") ;}} catch (exception e) {e. printstacktrace ();} // string message = getstring (R. string. gcm_message); displaymessage (context, message); // notifies user generatenotification (context, message );}
Do not forget to add required permissions to androidmanifest. xml.
After completion. You can install the program on your mobile phone, open the network of your mobile phone, and the program automatically registers. The device registration ID is displayed on the screen.
Then start writing the pushed code.
Gcmsend. Java
Import COM. google. android. GCM. server. message; import COM. google. android. GCM. server. result; import COM. google. android. GCM. server. sender; public class gcmsend {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubgcmsend send = new gcmsend (); send. sendmessage ("Hello World");} public void sendmessage (string MSG) {try {string serverkey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "; // serverkey is the value of the API keystring regid = "bbbbbbbbbbbbbbbbbbbbbbb" obtained after you create the server key by using API access in the API project; // the value obtained by the app registered with GCM on your device, gcmregistrar. getregistrationid (this); Sender sender = new sender (serverkey); message = new message. builder (). adddata ("MSG", MSG ). build (); Result result = sender. send (message, regid, 5); string status = "sent message to one device:" + result; system. out. println (Status);} catch (exception e) {e. printstacktrace ();}}}
Run this program on your computer to send the message ("Hello World") to your device.
Download the code (Change send_id, serverkey, and regid In the example to your value)
Android Receiver
GCM sender