Q:

Wakeful IntentService work incomplete thith Notification in sleep mode

I have set up AlarmManager for 1 minute for test mode. It sends broadcast to OnAlarmReceiver.
OnAlarmReceiver start sendWakefulWork(context, TaskService); and in TaskService in doWakefulWork() method i sendOrderedBroadcast which fire Notification.
This works well^ notification with sound and vibration appear. But only when phone in active mode. When phone in sleep mode (screen is switched off) only vibration and flash work during notification. No sounds from notification in sleep mode, only vibration. Sometimes it make sounds, but only sometimes , for example make 1 sound in hour, but it should make sound every minute. Also it make sounds every minute in sleep mode when phone connected to the computer but when I unplug phone from computer it starts only vibrate. When I turn on screen everything becomes normal: notification plays sound + vibration.
So I have no idea if it problem with my code or with my phone (HTC Desire Android 2.2)
I have tried a lot of things to fix it:

default and custom sounds
notification.audioStreamType = AudioManager.STREAM_SYSTEM and others
with flash and without flash
with vibration and without vibration

Nothing. Only sounds disappear in sleep mode, vibration worked perfect.
public class OnBootReceiver extends BroadcastReceiver {

public static void setAlarm(Context ctxt) {

           AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
   mgr.setRepeating(AlarmManager.RTC_WAKEUP,
           System.currentTimeMillis()+1000,
           1*60*1000,
           getPendingIntent(ctxt));
}

public static void cancelAlarm(Context ctxt) {
   AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
   mgr.cancel(getPendingIntent(ctxt));
}

private static PendingIntent getPendingIntent(Context ctxt) {
   Intent i=new Intent(ctxt, OnAlarmReceiver.class);
   return PendingIntent.getBroadcast(ctxt, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
}

@Override
public void onReceive(Context ctxt, Intent intent) {
   setAlarm(ctxt);
}
}

My doWakefulWork method in TaskService class:
protected void doWakefulWork(Intent intent) {
Intent mIntent = new Intent(EXERCISE_EVENT);
   mIntent.putExtra(StringUtils.PARAM_NEXT_TASK_TIME, nextStringTime);
   mIntent.putExtra(StringUtils.PARAM_TASK_NUMBER, taskNumber);

   sendOrderedBroadcast(mIntent, null);
}

And this is my NotificationBrodcastReceiver :
public class NotificationBrodcastReceiver extends android.content.BroadcastReceiver {

public void onReceive(Context ctx, Intent intent) {

           String ns = Context.NOTIFICATION_SERVICE;
           NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);

           int icon = R.drawable.notif_icon;
           CharSequence tickerText = ctx.getResources().getString(R.string.notification_ticker);
           long when = System.currentTimeMillis();

           Notification notification = new Notification(icon, tickerText, when);
           notification.flags |= Notification.FLAG_AUTO_CANCEL;

           notification.defaults |= Notification.DEFAULT_SOUND;
           notification.defaults |= Notification.DEFAULT_VIBRATE;

           Intent notificationIntent = new Intent(ctx, MainActivity.class);
           PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
           notification.setLatestEventInfo(ctx, "contentTitle", "contentText", contentIntent);
           mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}

A:

You are using an ordered broadcast to raise your Notification. Presumably, you are doing this because you want an activity to handle the request if the activity is in the foreground and has a higher-priority BroadcastReceiver for this broadcast.
That pattern is fine for non-wakeful situations. However, once doWakefulWork() ends, nothing is going to keep the device awake, and so the device will fall back asleep. Your ordered Broadcast happens asynchronously, so the its work will happen after doWakefulWork() ends, and so your Notification may not happen at all. If the Notification is partially happening, as you describe, I would personally consider this behavior to be a bug in Android (it should hold its own WakeLock for the purposes of playing back the ringtone), but it is entirely possible that your BroadcastReceiver simply does not get control at all, because the device falls asleep before the broadcast is delivered.
Your hold-a-WakeLock-for-two-seconds workaround will increase the odds of success, but that too is not guaranteed. The only guaranteed ways to ensure that you will get a chance to raise the Notification is to either:

do that work inside of doWakefulWork(), or
hold a WakeLock until the broadcast is completed and the Notification is raised.

I will look to add additional features to the WakefulIntentService framework in the future to handle your scenario better, as what you appear to want to do is reasonable, and it would be nice to have a common implementation of the pattern. However, what you are trying to do is definitely outside the current scope of what WakefulIntentService does.

