Notification Manager Activity new instance and resume activity

Blog >Lista Soluciones > Notification Manager

Hola programadores, os traigo un problemilla que me ha llevado unos días, ya que debido a la falta de info en la red, me ha traido varios quebraderos de cabeza. Expongo aquí una función que he comentado bastante para que se entienda, al llamar a esta función.

Crea una notificación en la persiana de notificaciones del teléfono Android, el código puede abrir una nueva instancia de la Activity o abrir la antigua en el método onPause()


oOo JAVA oOo
//FUNCION DE CREAR NOTIFICACION
   protected void LanzarNotificacion(){
//EL ID DE LA NOTIFICACION
        int notificationID = 1;
        //SE  CREA UN INTENT EN EL QUE SE GUARDA EL ID DE LA NOTIFICACION, SE LLAMA ASI MISMA
        Intent i = new Intent(this, MainActivity.class);
        i.putExtra("notificationID", notificationID);
        //EL PENDINGINTENT ES UN INTENT ADAPTABLE PARA QUE SE REALICE LA NOTIFICACION
        //MIENTRAS LA APLICACION PASA AL CICLO DE VIDA ONPAUSE()
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         
        //DETALLES
        CharSequence ticker ="Cronómetro Asalto Boxeo Activado";
        CharSequence contentTitle = "Cronómetro Asalto Boxeo";
        CharSequence contentText = "Cronómetro activado, sonará hasta que cierre la aplicación.";
        Notification noti = new NotificationCompat.Builder(this)
                                .setContentIntent(pendingIntent)
                                .setTicker(ticker)
                                .setContentTitle(contentTitle)
                                .setContentText(contentText)
                                .setSmallIcon(R.drawable.guante)
                                .addAction(R.drawable.guante,minuteroNotif+"  "+ticker, pendingIntent)
                                .setVibrate(new long[] {100, 250, 100, 500})
                                .build();
        
        //pa/ra que cuando se pulse desaparezca
        noti.flags= Notification.FLAG_AUTO_CANCEL;

        nm.notify(notificationID, noti);
        hayNotificacion=true;
    }


Y el resultado:





El problema de raíz, le tenía en que al pulsar sobre la notificación, me abría mi app pero en una nueva instancia, en vez de la anterior, que estaba en el ciclo de vida onPause()
Para que vuelva a abrir la instancia, en vez de una nueva:  En el Android Manifiest.xml añadir:

 oOo XML oOo
 
        
            /////////////////////////////////
            
                
                
            
        



Crea una notificación en la persiana de notificaciones del teléfono Android, que vuelve a primera instancia que se abrio de la Activity

Y en la función que lanza la actividad:
 oOo JAVA oOo
//FUNCION DE CREAR NOTIFICACION
   protected void LanzarNotificacion(){
//EL ID DE LA NOTIFICACION
        int notificationID = 1;
        //SE  CREA UN INTENT EN EL QUE SE GUARDA EL ID DE LA NOTIFICACION, SE LLAMA ASI MISMA
        Intent i = new Intent(this, MainActivity.class);
        //////////////////////////////////////////////////////////////////////////////////
        //AQUI ESTA LA LINEA PARA QUE SE LLAME A LA INSTANCIA DE LA ACTIVIDAD QUE ESTABA ONPAUSE();
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //////////////////////////////////////////////////////////////////////////////////
        i.putExtra("notificationID", notificationID);
        //EL PENDINGINTENT ES UN INTENT ADAPTABLE PARA QUE SE REALICE LA NOTIFICACION
        //MIENTRAS LA APLICACION PASA AL CICLO DE VIDA ONPAUSE()
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         
        //DETALLES
        CharSequence ticker ="Cronómetro Asalto Boxeo Activado";
        CharSequence contentTitle = "Cronómetro Asalto Boxeo";
        CharSequence contentText = "Cronómetro activado, sonará hasta que cierre la aplicación.";
        Notification noti = new NotificationCompat.Builder(this)
                                .setContentIntent(pendingIntent)
                                .setTicker(ticker)
                                .setContentTitle(contentTitle)
                                .setContentText(contentText)
                                .setSmallIcon(R.drawable.guante)
                                .addAction(R.drawable.guante,minuteroNotif+"  "+ticker, pendingIntent)
                                .setVibrate(new long[] {100, 250, 100, 500})
                                .build();
        
        //pa/ra que cuando se pulse desaparezca
        noti.flags= Notification.FLAG_AUTO_CANCEL;

        nm.notify(notificationID, noti);
        hayNotificacion=true;
    }



Bye Bye!! Haber si a más de uno le echa un cable!! salu2!!!

oOo mAkOnE oOo



Podeis descargar el código fuente de thebestandroide con sólo compartir en facebook,twitter,linkedin o suscribirte a nuestro canal RSS más abajo. 



Compartir Compartir Compartir Compartir




0 comentarios:

Publicar un comentario