For details, the technical principles of Play Services GCM in the Java library
Play Services GCM is part of the Google Cloud Messaging for the conveying messages between Android devices.It is a reliable and efficient message transmission service that allows developers to send messages to the application terminal device through the cloud.This article will introduce the technical principles of Play Services GCM in the Java class library and provide corresponding Java code examples.
Google Play Services GCM Technical Principles:
1. Registration device: The application needs to register the device to the GCM service.When registering, the application will send the unique identifier of the device to the GCM server.The GCM server generates a unique registration ID for each device and returns it to the application.
2. Send message: When the application needs to send messages to the device, it sends the message to the GCM server.The message includes the registration ID of the device, the information of the target device (for example, the operating system, application, etc.) and the content to be passed.The GCM server will find the target device based on the registration ID and pass the message to the GCM client of the device.
3. Device receiving message: A GCM client will run on each device to receive messages from the GCM server.The client regularly communicates with the GCM server to check whether there are new news.If there is a new message, the GCM server will push it to the device through the cloud message transfer service.
4. Processing message: Once the device receives the message, the GCM client will trigger a specific broadcast.Applications require the corresponding broadcast receiver to receive and process GCM messages.Once the message is received, the application can decide how to deal with it by itself.
Java code example:
1. Equipment registration:
GcmPubSub pubSub = GcmPubSub.getInstance(context);
String regToken = FirebaseInstanceId.getInstance().getToken();
pubSub.subscribe(regToken, "/topics/topic_name", null);
2. Send message:
JSONObject data = new JSONObject();
data.put("message", "Hello, World!");
String to = "<device registration ID>";
Result result = GcmServer.getInstance().send(data, to);
3. Receive and process messages:
public class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = extras.getString("message");
// Treat the message
}
}
The above example code shows the basic steps for equipment registration, sending messages, and receiving and processing messages.Developers can adjust and expand accordingly according to their needs and business logic.
In summary, the Play Services GCM in the Java class library provides a solution to achieve reliable and efficient message transmission. By communicating with the GCM server, the application can easily send and receive messages, thereby realizing the equipment between the devices.Real -time communication.