GCM推送通知DELAY

时间:2016-04-14 04:06:11

标签: android google-cloud-messaging

我真的卡在这里,当我发出推送通知时,消息延迟了大约10分钟。我检查logcat,只有当packageName没有被锁定时,消息才会直接到达。我怎么能控制这个?

非常感谢任何帮助。

这是我的logcat

logcat的

com.xxx.xxx D/MyGcmListenerService: From: 12345678
com.xxx.xxx D/MyGcmListenerService: Message: New Alert: #02 (ABC123)
com.xxx.xxx V/ContextImpl: ----- packageName = com.xxx.xxx is NOT LOCKED -----

清单

<!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->

<!-- [START gcm_receiver] -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.xxx.xxx" />
        </intent-filter>
    </receiver>
    <!-- [END gcm_receiver] -->


    <!-- [START instanceId_listener] -->
    <service
        android:name=".notification.MyInstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!-- [END instanceId_listener] -->



    <!-- [START gcm_listener] -->
    <service
        android:name=".notification.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->



    <service
        android:name=".notification.RegistrationIntentService"
        android:exported="false">
    </service>

服务器侧

pushNotification("xxxxxxx", "#02(hello)");

function pushNotification($registatoin_ids, $message) {

        // prepare variables for push notification
        $message = array("message" => "$message", "time_to_live" => 10000, "collapse_key" => "sample", "delay_while_idle" => true);
        $registatoin_ids = array("$registatoin_ids");

        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

        $GOOGLE_API_KEY = 'XXXXXXXXXX';
        $headers = array(
            'Authorization: key=XXXXXXXXXX',
            'Content-Type: application/json'
        );
        //print_r($headers);
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        // echo $result;
    }

MyGcmListenerService

    public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";
    private static final String MyPREFERENCES = "xxxx";
    private static final String JOB_KEY = "xxxx";

    private String JobStatus;

    int countNotification = 0;


    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        sendNotification(message);  
    }


    private void sendNotification(String message) {

        SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        JobStatus = sharedPreferences.getString(JOB_KEY, "");


        if (message.contains("#02")) {

             String submsg = message.substring(message.indexOf("(") + 1, message.indexOf(")"));

            Intent launch = new Intent(Intent.ACTION_MAIN);
            launch.setClass(getApplicationContext(), AnnouncementActivity.class);
            launch.addCategory(Intent.CATEGORY_LAUNCHER);
            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            launch.putExtra("message", submsg);
            startActivity(launch);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(0);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

尝试在PHP代码

中的数组$字段中添加&#39; priority' => 'high'
$fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
        'priority' => 'high'
    );

https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message

  

高优先级。 GCM尝试提供高优先级消息   立即,允许GCM服务唤醒睡眠设备   可能并打开与您的应用服务器的网络连接。应用程序   例如,通常的即时消息,聊天或语音呼叫警报   需要打开网络连接并确保GCM提供   消息延迟到设备。只有在设置时才设置高优先级   消息是时间关键的,需要用户立即   交互,并注意将您的邮件设置为高优先级   与普通优先级相比,电池消耗更多   消息。

答案 1 :(得分:1)

这是服务器端问题

您可以要求服务器方人员将其中一个属性更改为delay_while_idle=0

这将解决您的问题

答案 2 :(得分:0)

有一个已知的问题产生了这些症状,因此可能是您所看到的行为的原因。

在更便宜的wifi路由器上,连接会重置,因此在这些情况下,GCM仅在心跳间隔接收更新。

TL; DR:尝试不同的wifi路由器,看看你是否还有同样的问题。

有关详细信息,请参阅以下链接:

https://eladnava.com/google-cloud-messaging-extremely-unreliable/

这些间隔的问题是由网络路由器和移动运营商引起的,他们在几分钟不活动后断开空闲套接字连接。通常,这在廉价的家用路由器中更常见,其制造商决定空闲套接字连接的最大寿命,并终止它以节省资源。这些路由器只能处理有限数量的并发连接,因此采取这种措施来防止过载。这导致终止GCM套接字,

作为一种解决方法,请考虑Pushy(https://pushy.me),它是GCM / FCM的直接替代品,可大大提高通知速度和速度。可靠性(完全披露 - Elad Nava创立了Pushy)。