Search code examples
androidandroid-notificationsandroid-pendingintent

Check if notification is active


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

    toggleLigar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(toggleLigar.isChecked()){
                NotificationUtil.cancelaAll(TelaCriaDesp.this);

                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);
    //intent.setAction(Intent.);
    PendingIntent teste = PendingIntent.getActivity(context, id, intent,
                                                PendingIntent.FLAG_NO_CREATE);
    return teste  != null;
}

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

here's my notification creation:

   public class NotificationUtil {

@SuppressWarnings("deprecation")
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)
                                                    .setContentTitle(tickerText)
                                                    .setContentText(message)
                                                    .setSmallIcon(R.drawable.icon)
                                                    .setContentIntent(aoSelecionar);

        //montaNotification.setAutoCancel(true);
        montaNotification.setOngoing(true);

        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) 
                                  context.getSystemService(Activity.NOTIFICATION_SERVICE);

    manager.notify(id, notification);

}

Solution

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

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

    If You only have one simple notification of given type, You can just persist a boolean flag in a preference file without need for local database to use.