how to do the tap function on android notification

0

Hello, I have the following situation, it is a chat, and the user receives a message .. I want that when the app is minimized it receives an alert ... The part of the alert already did and it is working:

@JavascriptInterface
    public void notificacao(String mensagem){
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

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

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.app)
                .setContentTitle("Nova mensagem")
                .setContentText(mensagem)
                .setSound(soundUri); //This sets the sound to play

        notificationManager.notify(0, mBuilder.build());


    }

In javascript when I receive the message I call:

var notificacao_mensagem="mensagem a enviar";
window.JSInterface.notificacao(notificacao_mensagem);

It works, but when I click on the notification it does nothing .. I wanted to click on it the program that is minimized to appear again

    
asked by anonymous 29.12.2016 / 15:39

1 answer

1

The block is missing

mBuilder.setAutoCancel(true);

To close a notification programmatically, you must know the id of the notification and can cancel it using the cancel method

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(id_notificacao);

Alternatively, you can create an event with the addAction method to exit your notification.

Intent intent = new Intent(context, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(NOTIFICATION_ID, id_notificacao);
    PendingIntent pullOfIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

......

   mBuilder.setContentIntent(notifyPIntent);

......

.addAction(R.drawable.ic_action_cancel, "Sair", sairIntent)

references: link

    
29.12.2016 / 19:21