Reputation: 783
I gave it a couple attempts and none have worked. From my understanding doing this should hold the instance of my broadcast receiver running whether or not that app goes into the background, idles or closes. Once it's open the receiver keeps running (unless force closed?).
I am not sending delay_while_idle flag with my GCM inbound message below is an example of how I'm pushing the messages.
{
"data":
{
"alert": "message",
"badge":"0"
},
"registration_ids": ["1234567890abcdefghij"]
}
I'm still slightly confused on this topic but so far this is where I am at. NOTE This problem is just when I open the app, go to another or just by hitting home. After x amount of time the system I guess idles the app. Still running, just not actively. During this state my app will not process incoming GCM broadcasts when I need it to.
First I declare my Broadcast Receiver in my applications class.
public class MyApplication : global::Android.App.Application
{
public static MyGCMBroadcastReceiver BroadcastReceiver = new MyGCMBroadcastReceiver();
}
Below is my Broadcast Receiver.
[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(); is called after the code that produces the notification in my intentservice class. I did this because I though it was releasing the wake lock to soon.
Upvotes: 0
Views: 263
Reputation: 6078
For the receiver, you may extend the WakefulBroadcastReceiver
class instead of BroadcastReceiver
.
Helper for the common pattern of implementing a BroadcastReceiver
that receives a device wakeup event and then passes the work off to a Service
, while ensuring that the device does not go back to sleep during the transition.
For more details, you can refer to here.
Upvotes: 1