电话闲置时收到的GCM消息会延迟

时间:2016-03-09 12:59:00

标签: android amazon-web-services google-cloud-messaging amazon-sns

我在我的应用程序中使用Amazon Web Services。特别是向移动端点发送SNS消息。通过亚马逊官方和Google官方文档实现的所有功能都非常完美。但是几天我正在与一个奇怪的虫子作斗争。在一些非活动时间后收到的GCM消息有很大的延迟(10分钟)。如果我关闭/打开WiFi连接,之后我会立即收到所有消息。所以它看起来像WiFi /移动互联网进入睡眠状态,在这种状态下GCM会延迟接收。有人可以解释我怎么能唤醒互联网连接没有这个延迟?从我的视图关闭/开启wifi它不是优雅的解决方案:)

这是我的接收者代码:

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

也没有错误消息,只是一个很大的延迟。

我的清单文件。          

    <permission android:name="com.hssoft.cashassistmobileorders.permission.C2D_MESSAGE" android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.hssoft.cashassistmobileorders.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <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">
        <activity android:name=".MainActivity"
                  android:label="@string/app_ShortName" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <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.hssoft.cashassistmobileorders" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.hssoft.cashassistmobileorders.gcm.hsGCMListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name="com.hssoft.cashassistmobileorders.gcm.hsInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

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

在MainActivity中声明广播接收器:

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

1 个答案:

答案 0 :(得分:0)

从我在评论和问题中看到的,这就是答案。

如果在Activity类中声明BroadcastReceiver而不是在AndroidManifest.xml中声明的独立接收器,它将在应用程序死亡时死亡,并且在发送时不会被唤醒新的GCM推送。

您要做的是声明将通过清单公开的BroadcastReceiver,这样当新推送到达时它会唤醒您的应用