Create an alert dialog by clicking on a notification

0

As I do for when I play the notification instead of launching an activity I launch an alert dialog with the numbers?

private void showNotification( RealmResults<Note> notes, String key )
{
    //asignar inbox style notification
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle("Resultado de la búsqueda");
    for (Note note : notes) {
        inboxStyle.addLine(note.getDescription() + " - " + note.getEntity());
    }

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    //construir la notificicion
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_find_notification)
            .setContentTitle("Util-UCF búsqueda")
            .setContentText("Resultados de la búsqueda hecha con clave " + key)
            .setStyle(inboxStyle)
            .setSound(defaultSoundUri)
            .setVibrate(new long[] {100, 250, 100, 500})
            .setTicker("Búsqueda");
    //Obtener una instancia del servicio NotificationManager
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    //para poner la notificacion el la barra de notificaciones
    notificationManager.notify(REQUEST_CODE, builder.build());
}

Thanks for any help

    
asked by Alex Rivas 15.05.2018 в 15:30
source

1 answer

0

You need to add a PendingIntent to your notification:

//TuPropiaActivity es el activity que obtendrá el pendingIntent y se encargará de mostrar un dialog
Intent notificationIntent = new Intent(getApplicationContext(), TuPropiaActivity.class);
pendingIntent.putExtra("mostrar_dialog", true); //puedes agregar extras
pendingIntent.putExtra("note_description", note.getDescription); //extras

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

//**agrega tu propio requestID que será tu requestCode**
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

//construir la notificicion
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_find_notification)
        .setContentTitle("Util-UCF búsqueda")
        .setContentText("Resultados de la búsqueda hecha con clave " + key)
        .setStyle(inboxStyle)
        .setSound(defaultSoundUri)
        .setVibrate(new long[] {100, 250, 100, 500})
        .setTicker("Búsqueda")
        .setContentIntent(pendingIntent); //agrega tu pendingIntent al builder

Now, when the user gives a tap to the notification, the pendingIntent will be activated and will open "TuPropiaActivity", then, within the onCreate method of TuPropiaActivity write

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.hasExtra("mostrar_dialog")) {
        boolean showDialog = getIntent().getBooleanExtra("mostrar_dialog",
        false);
        if (showDialog) {
            String noteDescription = getIntent().getStringExtra("not_description", "default_text");
            //Muestra el dialog aquí
        }
    }
}

I hope at least I have managed to give you an idea ... Good luck

    
answered by 17.05.2018 в 23:17