Java – Google cloud messaging – instant or long received messages

I'm using Google cloud messaging in my last year's college project Everything is going well, but I've been having trouble in GCM Very regularly, messages can be delivered in real time or delayed for a long time

I've seen this, and I really don't think it applies to this situation:

In my project, up to 5 or 6 messages were sent from the server in one minute If GCM can be used for chat applications, are they sure they can't block messages sent / received at this rate? If my project works only 50% of the time, it becomes very annoying and will be very bad

This is the code of the message sent by the server:

@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 hope someone has a similar problem. I just want to make sure that the problem is what I'm doing or in my hands

tl; Dr GCM messages can arrive immediately or after about 10 minutes (delays are usually consistent)

Solution

The GCM framework part on the client phone uses TCP connection on port 5228 This connection is used to push notifications, but because each TCP connection can timeout with some routers / operators applying strict policies to kill inactive TCP connections (TCP idle timeout)

Most wireless routers kill invalid connections after 5 minutes, such as mine

The GCM framework uses the keep alive mechanism to send heartbeat network packets on WiFi every 15 minutes and every 28 minutes This hold activity is not always reliable for all users

I opened this question here: https://productforums.google.com/forum/# ! Category topic / nexus / connecting to networks and devices / fslyqyrulto they agree that there is a problem now

Editor (2014 / 01 / 08): at present, Google updates the heartbeat interval to 8 minutes for WiFi and mobile connections This is a remote change that affects all Android devices. 2.2 this is a good improvement to avoid TCP push connection timeout However, if the WiFi router kills the invalid connection after 5 minutes, the push notification will be delayed by 3 (8-5) minutes (if there are no other notifications to keep the connection)

Edit (2016 / 03 / 06): now Google seems to be testing my feedback two years ago. There is a dynamic mechanism to determine the correct heartbeat interval depends on the network At present, it seems to be phased out and only applies to WiFi, as far as I know Therefore, based on WiFi SSID, the algorithm determines the correct heartbeat interval of a specific WiFi through gradual refinement That sounds good! This is a remote change that affects every Android phone with Google play service

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>