public void testNotify() { mNotificationManager.cancelAll(); final int id = 1; sendNotification(id, R.drawable.black); // test updating the same notification sendNotification(id, R.drawable.blue); sendNotification(id, R.drawable.yellow); // assume that sendNotification tested to make sure individual notifications were present StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications(); for (StatusBarNotification sbn : sbns) { if (sbn.getId() != id) { fail("we got back other notifications besides the one we posted: " + sbn.getKey()); } } }
private boolean checkNotificationExistence(int id, boolean shouldExist) { // notification is a bit asynchronous so it may take a few ms to appear in // getActiveNotifications() // we will check for it for up to 200ms before giving up boolean found = false; for (int tries = 3; tries-- > 0; ) { final StatusBarNotification[] sbns = mNotificationManager.getActiveNotifications(); for (StatusBarNotification sbn : sbns) { if (sbn.getId() == id) { found = true; break; } } if (found == shouldExist) break; try { Thread.sleep(100); } catch (InterruptedException ex) { // pass } } return found == shouldExist; }