应用程序关闭时无法接收GCM

时间:2014-01-15 07:49:46

标签: android broadcastreceiver google-cloud-messaging google-talk

我创建了一个从我的服务器接收GCM消息的应用程序。我试图以谷歌的样本(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消息。

2 个答案:

答案 0 :(得分:0)

像这样 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" />

答案 1 :(得分:0)

发送gcm消息时设置delay_while_idle=0