Android 手机空闲时延迟接收GCM消息
我正在我的应用程序中使用Amazon Web服务。特别是向移动终端发送SNS消息。所有的东西都是从亚马逊和谷歌的官方文档中实现的,而且都非常完美。但几天来我和一只奇怪的虫子搏斗。经过一段非活动时间后,收到的GCM消息延迟很长(10分钟)。如果我关闭/打开WiFi连接,之后我会立即收到所有消息。因此,WiFi/移动互联网似乎进入睡眠状态,在这种状态下,GCM会延迟接收。有人能解释一下我如何唤醒互联网连接以避免延迟吗?在我看来,关闭/打开wifi不是一个优雅的解决方案:)Android 手机空闲时延迟接收GCM消息,android,amazon-web-services,google-cloud-messaging,amazon-sns,Android,Amazon Web Services,Google Cloud Messaging,Amazon Sns,我正在我的应用程序中使用Amazon Web服务。特别是向移动终端发送SNS消息。所有的东西都是从亚马逊和谷歌的官方文档中实现的,而且都非常完美。但几天来我和一只奇怪的虫子搏斗。经过一段非活动时间后,收到的GCM消息延迟很长(10分钟)。如果我关闭/打开WiFi连接,之后我会立即收到所有消息。因此,WiFi/移动互联网似乎进入睡眠状态,在这种状态下,GCM会延迟接收。有人能解释一下我如何唤醒互联网连接以避免延迟吗?在我看来,关闭/打开wifi不是一个优雅的解决方案:) <permi
<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>
这是我的接收器代码:
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>
此外,没有错误消息,只是延迟了很长时间
<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>
我的清单文件。
<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>
从我在评论和问题中看到的情况来看,这就是答案
<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>
如果您在Activity类中声明了一个BroadcastReceiver
,而不是在AndroidManifest.xml
中声明的一个独立接收器,则该接收器将在应用程序死亡时死亡,并且在发送新的GCM推送时不会被唤醒
<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>
您要做的是声明一个将通过清单公开的
BroadcastReceiver
,这样当新推送到达时它将唤醒您的应用程序您确定客户端和服务器上的实现符合文档要求吗?绝对正确。正如我之前所写的,在设备进入睡眠状态之前,我会立即收到所有消息,但在睡眠后,我必须等待延迟或重新连接internet连接。这是预期的行为,您是否有注册的广播接收器
?主活动中有。但这个逻辑的第一点是在我们发送广播消息之后接收到的onMessageReceived。在我的情况下,OnMessageReceived中没有任何传入消息。我已经更新了我的第一篇帖子。正如您所看到的,清单中描述了所有需要的服务。但我想重复一次,问题不在广播接收机。hsGCMListenerService出现问题,在WiFi进入睡眠状态后,此服务会延迟(10分钟)接收消息。与Gmail和其他使用GCM的应用程序一样,经过一段时间没有任何动作后,我会延迟收到通知,当我关闭/打开互联网连接时,我会立即收到所有通知。我可以通过编程方式重新连接,但从我的观点来看,应该是更优雅的解决方案:)
<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>