3

i use following code in GCMIntentService.java

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wl.acquire(15000);
    wl.acquire();

but push notification not show in sleep mode.

Push notification working fine when i unlock my phone , then receive notification properly.

but i want to show notification in sleep mode, as like sound play and receive notification from gmail app. please help how can i do it .

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message" + intent.getStringExtra("message"));
    String message = intent.getStringExtra("message");// getString(R.string.gcm_message);
    generateNotification(context, message);
}

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.logo;
    long when = System.currentTimeMillis();


    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, Tabs.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound);
    notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;


    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    //notification.defaults = Notification.DEFAULT_ALL;
    notificationManager.notify(0, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wl.acquire(15000);



}
Rohit
  • 3,401
  • 4
  • 33
  • 60

1 Answers1

0

Try this:

    private static void generateNotification(Context context, String message) {
    int icon = R.drawable.notification_icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);        
    String title = context.getString(R.string.app_name);
   // Log.i(TAG, "Received reg id:=>"+regId);
    Intent notificationIntent = new Intent(context, Message.class);
    notificationIntent.putExtra("Message", message); 

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      
    Constant.iMessageParsing = 0;

}

call this method in your onMessage(Context context, Intent intent) method of GCMIntentService class.

Try this code::

PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", ""+isScreenOn);

if(isScreenOn==false)       
   {
     WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");                            
     wl.acquire(10000);
     WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
     wl_cpu.acquire(10000);
   }
Karan Maru
  • 991
  • 6
  • 17
  • 1
    i have checked in debugging , debugger not goes to function when request send from server, debugger comes into function when i unlock screen – Rohit Dec 09 '13 at 09:10