微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Google Cloud Messaging – 即时或长时间收到的消息

我在大学的最后一年的项目中正在使用Google云端消息.一切顺利,但是我一直在GCM中遇到麻烦.很定期地,消息可以实时交付,或者是延迟很长时间.

我已经看过这个,我真的不认为这适用于这种情况:

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.

在我的项目中,一分钟就从服务器上发出了最多5或6条消息.如果GCM可以用于聊天应用程序,肯定他们无法阻止以这种速率发送/接收的消息?如果我的项目只在50%的时间工作,那么它变得非常烦人,会非常糟糕

这是服务器发送的消息的代码

@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();
            }
        }
}

和我的Android onMessage方法

@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();

        }
    });
    */
}

我希望有人有类似的问题,我只想确认问题是我正在做的事情,或者是我的手中.

tl; dr GCM消息可以立即到达或大约10分钟后(延迟通常是一致的).

解决方法

客户端电话上的GCM框架部分在端口5228上使用TCP连接.此连接用于推送通知,但是由于每个tcp连接可以与应用严格策略的一些路由器/操作符进行超时,以杀死非活动的tcp连接( tcp空闲超时).

大多数无线路由器在5分钟之后杀死了无效连接,例如像我的.

GCM框架使用保持活动机制,每隔15分钟在WiFi上每隔28分钟发送一次心跳网络数据包.这种保持活动对于所有用户来说并不总是可靠的.

在这里打开了这个问题:
https://productforums.google.com/forum/#!category-topic/nexus/connecting-to-networks-and-devices/fslYqYrULto
他们同意现在有一个问题.

编辑(2014/01/08):目前,Google将心跳间隔更新为8分钟,用于WiFi和移动连接.这是一个影响所有Android设备的远程更改2.2
这是一个很好的改进,以避免tcp push连接超时.不过,如果WiFi路由器在5分钟后杀死了无效连接,则推送通知将延迟3(8-5)分钟(如果没有其他通知保持连接)

EDIT(2016/03/06):现在谷歌似乎正在测试我的2年前的反馈,有一个动态机制来确定正确的心跳间隔取决于网络.目前似乎是分阶段推出,只适用于WiFi,因为我所知道的.因此,基于WiFi SSID,该算法通过逐步细化来确定特定WiFi的正确心跳间隔.这听起来很不错!这是一个偏远的变化,影响每个具有Google Play服务的Android手机.

原文地址:https://www.jb51.cc/java/124796.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐