I am using Google cloud messaging in my final year project for college. Everything works okay but I have been having a bit of trouble with GCM. Pretty regularly, messages are either delivered virtually instantly, or with a large delay.
I have read this and I really don't think it applies in this case:
GCM will usually deliver messages immediately after they are sent. However, this might not always be possible. For example, the device could be turned off, offline, or otherwise unavailable. In other cases, the sender itself might request that messages not be delivered until the device becomes active by using the delay_while_idle flag. Finally, GCM might intentionally delay messages to prevent an application from consuming excessive resources and negatively impacting battery life.
In my project were talking about at most 5 or 6 messages from the server a minute. If GCM can be used for a chat application surely they couldn't block messages which are sent / received at this rate? It has become pretty annoying and will pretty badly if my project only works 50% of the time...
Here is my code for the message the server sends:
@Override
public void run() {
Message.Builder messageBuilder = new Message.Builder().delayWhileIdle(false);
Gson gson = new Gson();
messageBuilder.addData("profile", gson.toJson(profile));
databaseConnection.notifyDevices(messageBuilder.build());
}
public void notifyDevices(Message message) {
Sender sender = new Sender(xxx);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("message", message.toString()));
//LOG
System.out.println("Notifying devices with the following message \n \"" +message+ "\"");
List<String> deviceIDsList = new ArrayList<String>();
String [] deviceIDArray;
//Get devices to notify
List<JSONDeviceProfile> deviceList = getDevicesToNotify();
for(JSONDeviceProfile device : deviceList) {
deviceIDsList.add(device.getDeviceId());
try {
sender.send(message, device.getDeviceId(), 5);
} catch (IOException e) {
System.out.println("Error sending GCM message!");
e.printStackTrace();
}
}
}
And my Android onMessage method:
@Override
protected void onMessage(Context arg0, Intent intent) {
String message = intent.getStringExtra("profile");
Log.d(TAG + "Received Message: ", "Received Message: " + message.toString());
//CALL NEW INTENT WITH PROFILE DETAILS
Intent displayProfileIntent = new Intent(arg0, DisplayProfile.class);
displayProfileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
displayProfileIntent.putExtra("message", message);
startActivity(displayProfileIntent);
/*
//generateNotification(arg0, username);
handler.post(new Runnable() {
@Override
public void run() {
//Toast.makeText(getApplicationContext(), username, Toast.LENGTH_SHORT).show();
}
});
*/
}
I am hoping somebody has had a similar issue, I just want to confirm that the problem is something I am doing or if it is out of my hands.
tl;dr GCM messages either arrive instantly or about 10 minutes later (the delay is usually consistent).