问题
I have downloaded and setup the Android gcm-client and gcm-demo-server (http server) from http://code.google.com/p/gcm/ . I have managed to receive downstream messages in my Android gcm-client app. I have done numerous tests with the following characteristics:
- The server used either a browser API key or a server API key.
- The data connection was either 3G or WiFi
- The sent message included/didn't include additional data.
- The sent message had/didn't have a collapse key.
- The sent message had delayWhileIdle set to false.
- The sent message had timeToLive set to 0 or 5 minutes.
- The server was located in two different locations within Europe.
- The Android app run in two devices located in different places within Europe.
In all cases the app receives the sent messages after 25-30 sec from the moment they were sent. However, I have tested other Android applications that use GCM downstream messages and in most cases the messages arrive almost instantly (<5sec).
Is there something I can change in the client or server in order to have the messages received in the app sooner?
I also tried to use the gcm XMPP server (Java application using Smack) given in http://developer.android.com/google/gcm/ccs.html but I didn't manage to sent any messages. The response I receive from GCM Cloud Connection Server (CCS) says "Project XXXXXXXXXX not whitelisted". The most possible reason is that my registration for CCM has not been approved yet as described in Google CCS (GCM) - project not whitelisted . As I have read in various websites, the approval usually takes more than 2-3 months, so I will most possibly not be able to test the XMPP server soon.
Is it possible that you can only receive nearly instant messages by using an XMPP server? Does anybody have any experience of the delays encountered using an XMPP server?
Thanks in advance
回答1:
I think that the problem is in the android gcm-client demo that are retaining the message, not in the gcm-server. In GcmIntentService.java file of gcm-client demo, it can be seen that the function protected void onHandleIntent(Intent intent) have the following code:
[...]
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++) {
Log.i(TAG, "Working... " + (i + 1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
[...]
This code sleeps android application 5 seconds every iteration (total 25 seconds) before deliver a notification message. You can delete this code due to is an example and you can insert into else if statement some work before show notification.
I have implemented a xmpp server and I receive the notification in <5 seconds, without this working code, of course.
Hope this help and sorry for my bad English.
来源:https://stackoverflow.com/questions/21259327/google-cloud-messaging-gcm-downstream-message-delay