Rafael Kehl Rafael Kehl - 12 days ago 5x
Android Question

Check if notification is active

i'm building a notification that is launched with a toggle button:

toggleLigar.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

CharSequence tickerText = "Despertador ativado!";
CharSequence title = "Você não vai perder seu ponto!";
CharSequence message = "O despertador " + despertador.getNomeDesp() + " está ativo!";
Intent intent = new Intent(TelaCriaDesp.this, TelaCriaDesp.class);

NotificationUtil.criaNotification(TelaCriaDesp.this, tickerText, title, message,
despertador.getDesp_id(), intent);
} else {
NotificationUtil.cancelaNotification(TelaCriaDesp.this, despertador.getDesp_id());

now i want to assign the toggle button in ON or OFF according to the notification state, if it's active (being displayed) it's ON, inactive OFF... I have tried this:

public static boolean testaNotification(int id, Context context){
Intent intent = new Intent(context, TelaCriaDesp.class);
PendingIntent teste = PendingIntent.getActivity(context, id, intent,
return teste != null;

but it's not working, it always returns NULL...

here's my notification creation:

public class NotificationUtil {

public static void criaNotification(Context context, CharSequence tickerText, CharSequence title,
CharSequence message, int id, Intent intent){

PendingIntent aoSelecionar = PendingIntent.getActivity(context, 0, intent, 0);

Notification notification = null;
int apiLevel = Build.VERSION.SDK_INT;

if(apiLevel >= 11){
Builder montaNotification = new Notification.Builder(context)


if(apiLevel >= 17){
notification = montaNotification.build();
} else {
notification = montaNotification.getNotification();
} else {
notification = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());

notification.setLatestEventInfo(context, title, message, aoSelecionar);
//notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_ONGOING_EVENT;

NotificationManager manager = (NotificationManager)

manager.notify(id, notification);



You cannot. You must track it manually if you need. In fact Notification is meant only to notify a user abot some event. If you need to check if the notification is present - then in fact you need to check if the event (about which is the notification) is still valid.

In your case you may persist information in local database when creating a notification. Add a contentIntent to the notification to be able to mark the database item appropriately when user dismisses notification by clicking it. Then when you need to test if notification exists just look for a valid row in local db.

If you only have one simple notification of that type you can just persist a boolean flag in preference file.