how to make tap function in android notification

Asked

Viewed 68 times

0

Hello, I have the following situation, is a chat, and the user receives a message.. I want that when the app is minimized it receives an alert... The alert part I’ve done and it’s 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 receiving the message I call:

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

It works, only when I click on the notification it does nothing.. I wish that by clicking on it the program that is minimized would return to appear

1 answer

1

Missing add the block

mBuilder.setAutoCancel(true);

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

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

notificationManager.cancel(id_notificacao);

Alternatively, you can create an event with the method addAction 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: https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.