Reputation: 1026
I have facing problem in getting notification message from GCM Server.Device will get notification correctly when it not idle or in running state but when device goes idle for 10-15 minutes at that time device not able to get notification and also all registered devices are not gets notification from GCM server.How to resolve this problem?
Upvotes: 1
Views: 464
Reputation: 41
Normally, your app need to wake when it sleeps.
Put this into your manifest file to wake your device when the message is received
<uses-permission android:name="android.permission.WAKE_LOCK" />
Add java class name WakeLocker.java
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context context) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "WakeLock");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
Call the above code in 'private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()' that might be in your MainActivity.java
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
Thank This source
Hope this helps
Upvotes: 1