GCM messages received with delay when phone idle
<p>I'm using Amazon Web Services in my application. Especially sending SNS messages to mobile endpoint. All things realized from official Amazon and Google documentation and works perfect. But few days i'm fighting with one strange bug. After some inactive time GCM messages received with huge delay(10 min). If i turn off/on WiFi connection, after that i receive all messages immediatelly. So it looks like WiFi/Mobile internet going to sleep and in this state GCM receive with delay. Can somebody explain how can i wakeup internet connection for to not have this delay? From my view turn off/on wifi it's not elegant solution :)</p>

<p>Here is my receiver code:</p>

<pre><code>    public class hsGCMListenerService extends GcmListenerService {    
        @Override
        public void onMessageReceived(String from, Bundle data) {
            Log.d(TAG, "SNS message received: "+ data.toString()); 
            doProcessSNSMessageAndInformMainActivity(data);
        }
    }

private void doProcessSNSMessageAndInformMainActivity(Bundle AMessage) {
   Intent msgReceived = new Intent(AppCommon.SNS_BROADCAST_MSG);  
   msgReceived.putExtra(AppCommon.C_SNSRequest_Result, AppCommon.C_SQSRequest_UploadDataOnS3);   

   LocalBroadcastManager.getInstance(this).sendBroadcast(msgReceived);
}
</code></pre>

<p>Also thre's no errors messages, just a big delay.</p>

<p>My manifest file.
    
    </p>

<pre><code>    &lt;permission android:name="com.hssoft.cashassistmobileorders.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt;

    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
    &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
    &lt;uses-permission android:name="com.hssoft.cashassistmobileorders.permission.C2D_MESSAGE" /&gt;
    &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;

    &lt;application
        android:name=".common.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_FullName"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"&gt;
        &lt;activity android:name=".MainActivity"
                  android:label="@string/app_ShortName" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
                &lt;category android:name="com.hssoft.cashassistmobileorders" /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

        &lt;service
            android:name="com.hssoft.cashassistmobileorders.gcm.hsGCMListenerService"
            android:exported="false" &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;

        &lt;service
            android:name="com.hssoft.cashassistmobileorders.gcm.hsInstanceIDListenerService"
            android:exported="false"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="com.google.android.gms.iid.InstanceID" /&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;

        &lt;service
            android:name="com.hssoft.cashassistmobileorders.gcm.RegistrationIntentService"
            android:exported="false"&gt;
        &lt;/service&gt;
        &lt;service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" /&gt;
    &lt;/application&gt;   
&lt;/manifest&gt;
</code></pre>

<p>Declaring Broadcast receiver in MainActivity:</p>

<pre><code>@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //https://developer.android.com/training/scheduling/wakelock.html#screen
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                AppCommon.doFillSettings();
                if (AppCommon.WorkDeviceIsConfigured) {
                    doSendSQSRequest(AppCommon.C_SQSRequest_RegistrateDevice);
                }

                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                if (!sharedPreferences.getBoolean(AppCommon.SENT_TOKEN_TO_SERVER, false)) {
                    Log.e("MainActivity", "mRegistrationBroadcastReceiver. Token creation problem");
                }
            }
        };

        mSNSBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.hasExtra(AppCommon.C_SNSRequest_Result)) {
                    doRefreshData(intent.getExtras().getInt(AppCommon.C_SNSRequest_Result));
                }
            }
        }; .....
</code></pre>