应用关闭时无法接收 GCM
Posted
技术标签:
【中文标题】应用关闭时无法接收 GCM【英文标题】:Could not receive GCM when app was closed 【发布时间】:2014-01-15 07:49:45 【问题描述】:我创建了一个应用程序,它从我的服务器接收 GCM 消息。我试图实现为 google 的示例 (http://developer.android.com/google/gcm/client.html#sample-receive)。除非出现问题,否则它可以正常工作。也就是说,如果我在内存中杀死了我的应用程序,我的广播接收器将永远不会收到 GCM 消息。 我尝试获取设备日志,发现此问题与 GTalkService 有关:
01-15 14:19:40.509: W/GTalkService(1300): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent act=com.google.android.c2dm.intent.RECEIVE pkg=com.paktor (has extras)
01-15 14:19:40.750: W/GTalkService(1300): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent act=com.google.android.c2dm.intent.RECEIVE pkg=com.paktor (has extras)
01-15 14:19:41.090: W/GTalkService(1300): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent act=com.google.android.c2dm.intent.RECEIVE pkg=com.paktor (has extras)
我的接收器在 manifest.xml 中:
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.paktor" />
</intent-filter>
</receiver>
更新: 我的广播:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Log.i(this, "Receive GCM message");
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
和权限:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
我不知道为什么。每件事看起来都很正常。请帮助我了解如何在我的应用程序被杀死时捕获 GCM 消息。
【问题讨论】:
我认为你需要在 android 中看到 WakeLocker。 developer.android.com/reference/android/os/… 我加了android.permission.WAKE_LOCK没问题 【参考方案1】:像这样制作WakefulBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
还有权限
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
【讨论】:
我之前是这样实现的。我想这不是问题【参考方案2】:发送gcm消息时设置delay_while_idle=0
【讨论】:
以上是关于应用关闭时无法接收 GCM的主要内容,如果未能解决你的问题,请参考以下文章