if my mobile is in screenlock, the GCMBroadcast receiver doesn’t send the message to the GCMIntentService.
I’ve read that the solution is to implement the trick of wakelock.acquire() and wakelock.release() in the onReceive method of my broadcast receiver.
BUT the GCMBroadcastReceiver has the onReceive in final, so i can’t add, this feature.
Is there a solution?
[EDIT]
Wrong Question, after 5 hours of investigation, i’ve solved everything.
To clarify:
- GCMBroadcast has the wakelock acquire and release, no needed any
implementation. -
My problem was due to the DELAY WHEN IDLE property set as true in the SERVER CODE.
builder = builder.delayWhileIdle(true);
From official documentation:
if the device is connected but idle, the message will still be
delivered right away unless the delay_while_idle flag is set to true.
Otherwise, it will be stored in the GCM servers until the device is
awake.
[/EDIT]
here my code:
GCMIntentService
public class GCMIntentService extends GCMBaseIntentService{
public GCMIntentService() {
super(CommonStuff.SENDERID);
// TODO Auto-generated constructor stub
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println("onError:"+arg1);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i("GCMIntentService", "Message Received");
Bundle extras = intent.getExtras();
// Extract the payload from the message
if (extras != null) {
String type=(String)extras.get("type");
String msg=(String)extras.get("message");
//do something........
}
}
@Override
protected void onRegistered(Context ctx, String regId) {
Log.d("GCMIntentService","REGID_ARRIVED:"+regId);
try {
//do something
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println("onUnregistered:"+arg1);
}
}
the wakeLocker
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context ctx) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
my GCMreceiver in MANIFEST
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my.app.package" />
</intent-filter>
</receiver>
Just to set as solved, the solution in the edit part of question.