0

我給它一個幾次嘗試,沒有工作過。根據我的理解,這應該讓廣播接收器的實例運行,無論該應用是否進入後臺,空閒或關閉狀態。一旦打開,接收機就會繼續運行(除非強制關閉?)。失敗嘗試接收GCM通知時,應用程序空轉

我不發送delay_while_idle標誌與我的GCM入下面的消息是,我如何推出這些消息的一個例子。

{ 
    "data": 
    { 
     "alert": "message", 
     "badge":"0" 
    }, 
    "registration_ids": ["1234567890abcdefghij"] 
} 

我對這個主題仍然有些困惑,但到目前爲止,這是我所處的位置。 注意這個問題只是當我打開應用程序,轉到另一個或只是打到家裏。經過x次的時間之後,我猜系統閒置了應用程序。仍在運行,只是不積極。在此狀態期間,我的應用在我需要時不會處理傳入的GCM廣播。

首先我聲明我的廣播接收器在我的應用程序類。

public class MyApplication : global::Android.App.Application 
{ 

    public static MyGCMBroadcastReceiver BroadcastReceiver = new MyGCMBroadcastReceiver(); 

} 

下面是我的廣播接收器。

[BroadcastReceiver(Permission = "com.google.android.c2dm.permission.SEND")] 
[IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { Configuration.Current.PackageName })] 
[IntentFilter(new string[] { "com.google.android.c2dm.intent.REGISTRATION" }, Categories = new string[] { Configuration.Current.PackageName })] 
[IntentFilter(new string[] { "com.google.android.gcm.intent.RETRY" }, Categories = new string[] { Configuration.Current.PackageName })] 
public class MyGCMBroadcastReceiver : BroadcastReceiver 
{ 

    private PowerManager.WakeLock wakeLock = null; 

    public override void OnReceive(Context context, Intent intent) 
    { 
     AcquireWakeLock(context); 

     MyIntentService.RunIntentInService(context, intent); 

     SetResult(Result.Ok, null, null); 
    } 

    public void AcquireWakeLock(Context context) 
    { 
     PowerManager powerManager = PowerManager.FromContext(context); 

     wakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial, "GCM Broadcast Receiver Tag"); 

     wakeLock.Acquire(); 
    } 

    public void ReleaseWakeLock() 
    { 
     if (wakeLock != null) 
      wakeLock.Release(); 
    } 

} 

MyApplication.BroadcastReceiver.ReleaseWakeLock();在我的intentservice類中生成通知的代碼後調用。我這樣做是因爲我雖然很快就釋放了喚醒鎖。

+0

是否能夠成功發送消息...?我的意思是在發送消息之後,你已經徹底檢查了從gcm收到的響應消息...? – 2015-01-15 16:39:28

+0

是的,該部分的作品。測試它的絕對方法是通過在前臺應用程序,推送消息,並立即顯示10次中的9次。但是當我從這裏打回家,等一會兒。通知從不顯示。因此,可能收到GCM廣播,但intentservice無法處理並初始化通知? – 2015-01-15 16:53:06

+1

對於接收器,你可能會延長「WakefulBroadcastReceiver」類,而不是「廣播接收器」 – bjiang 2015-01-15 17:51:15

回答

1

對於接收器,你可以擴展,而不是BroadcastReceiverWakefulBroadcastReceiver類。

幫助器實現BroadcastReceiver的常見模式,它接收設備喚醒事件,然後將工作關閉至Service,同時確保設備在轉換期間不會恢復休眠。

有關詳細信息,你可以參考here

相關問題