0

我製作了一個正在從我的服務器接收GCM消息的應用程序。我試圖實現谷歌樣本(http://developer.android.com/google/gcm/client.html#sample-receive)。除非是問題,否則它正常工作。這就是說,如果我在內存中殺了我的應用程序,我的廣播接收器將永遠不會收到GCM消息。
我試圖讓設備的日誌,看看這個問題是關於GTalkService:
關閉應用程序時無法接收GCM

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消息。

+0

我想你需要看WakeLocker在Android的設置delay_while_idle=0。 http://developer.android.com/reference/android/os/PowerManager.WakeLock.html – alicanbatur

+0

我添加了android.permission.WAKE_LOCK這不是問題 –

回答

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

許可

+0

我以前這樣實現。我想這不是問題 –

相關問題