I use notifications in my simple timer application (early testing phase) and found that maybe from 5% to 20% of the cases when I made a notification, the sound did not play. The notification appeared correctly in all other respects, including the LED flash, but was silent. I have confirmed with the help of temporary additional log lines that the sound URIs that I use are reasonable and actually identical in cases where work and cases that do not. I will summarize the code that sets up the notification in case I do something monumental stupid:
NotificationManager notifications = ...;
Notification n = new Notification(R.drawable.icon, text, nextMillis);
n.setLatestEventInfo(...);
n.flags = Notification.FLAG_NO_CLEAR | FLAG_SHOW_LIGHTS;
n.ledOnMS = 250; n.ledOffMS = 1250; n.ledARGB = 0xff2222ff;
n.audioStreamType = AudioManager.STREAM_ALARM;
n.sound = myUri;
notifications.notify((int)id, n);
Here is the real thing . The only common factor that I found is that in problematic cases the phone was asleep (the screen is off for more than 30 seconds). A notification is requested from the BroadcastReceiver , waking up from the RTC_WAKEUP Alarm . The failure mode that comes to mind is that the device starts to sleep too early again, but nothing in logcat says it. In fact, I see no difference in logcat between success and missing sound. In an attempt to get around sleep problems, if any, I added (in this commit ) this WakeLock temporarily:
+ PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timer");
+ wl.acquire();
+ Log.d(TimerActivity.TAG, "Got wake lock");
notifications.notify((int)id, n);
Log.d(TimerActivity.TAG, "Notified!");
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) { e.printStackTrace(); }
+ Log.d(TimerActivity.TAG, "Released wake lock");
+ wl.release();
... , , , , , , , ( ).
Nexus Ones Android 2.2 (Froyo). 2.1, 1.6 G1, . , SD- . 2011 Android 2.2.1 SD-. Android bug tracker , .
, , . , SD-; URI, .