GCM推送通知DELAY

1 人关注

我真的卡在这里了,当我做一个推送通知时,信息被延迟了10分钟左右。我在logcat上检查,只有当packageName不被锁定时,信息才会直接到达。我怎样才能控制这个问题呢?

真的很感谢任何帮助。

这是我的日志猫

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>

ServerSide

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);
    
2 个评论
你能分享一下你的GcmListenerService类的代码和发送数据到Gcm的服务器端代码吗?
已按要求更新
android
google-cloud-messaging
Nizzam
Nizzam
发布于 2016-04-14
3 个回答
itsa04g9
itsa04g9
发布于 2016-04-14
已采纳
0 人赞同

尝试在php代码中的数组$fields上添加' 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能够在您的应用程序服务器上打开网络连接。 需要打开网络连接,并确保GCM将信息及时传递给设备。 讯息传递给设备而不拖延。只有当信息是时间紧迫的,并需要在短时间内完成时,才可以设置高优先级。 只有当信息是时间紧迫的,需要用户立即互动时,才设置高优先级。 并注意将您的信息设置为高优先级 要注意的是,与普通的优先级信息相比,将信息设置为高优先级会造成更多的电池消耗。

为什么我们需要设置优先级? 每天的请求限制是什么? 对不起,请问
在谷歌用GCM取代C2DM之后,他们取消了所有的限制。因此,每天的请求没有限制。默认的优先级是 "正常",当你希望GCM服务在可能的情况下唤醒沉睡的设备并打开与你的应用服务器的网络连接时,你需要将优先级设置为 "高",换句话说,如果你希望你的通知在设备被锁定时尽快到达,只需将优先级设置为 "高
现在我得到了清晰的信息,谢谢你的帮助。另外,你的方法很有效。
我的回复还不够多,一旦我的回复足够多,我就会给你加分。) 我可以再问你一个问题吗,或者我们可以通过电子邮件联系。
当然,请在这里告诉我你的问题,如果我知道,我会回答你。)
Hardy Android
Hardy Android
发布于 2016-04-14
0 人赞同

It's serverside issue

你可以要求服务器端的人把它的一个属性改为 delay_while_idle=0

This will solve your issue

这与delay_while_idle=0有关吗?
是的,当这个标志为1时,它将等待,直到设备唤醒,当它为0时,它将直接发送至设备。
delay_while_idle已被弃用。 2016年11月15日之后,它将被GCM接受,但被忽略。 developers.google.com/cloud-messaging/http-server-ref
Mtl Dev
Mtl Dev
发布于 2016-04-14
0 人赞同

有一个已知的问题,正是产生这些症状的,因此是 could 是导致你所看到的行为的原因。

在便宜的WIFI路由器上,连接会被重置,所以在这种情况下,GCM只能在心跳间隔时间内收到更新。

TL;DR:在不同的WIFI路由器上试试,看看是否仍有同样的问题。