Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss notification on action click?

Tags:

android

I want to dismiss my notification, when clicked on dismiss from the notification . Just like we dismiss it on swipe.

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

    Intent intent = new Intent(ctx, MainActivity.class);
    intent.putExtra("isFromBadge", false);

    Intent resultIntent = new Intent(ctx, ResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
    stackBuilder.addParentStack(ResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

  //  PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new Notification.Builder(ctx)
            .setContentTitle(
                    ctx.getResources().getString(R.string.app_name))
            .setContentText(message)
            .setWhen(when)
            .setStyle(new Notification.BigPictureStyle().bigPicture(result))
            .setSmallIcon(R.mipmap.ic_launcher)
            .addAction(R.drawable.bell, "Dismiss", resultPendingIntent)
            .addAction(R.drawable.bell, "Share", contentIntent)
            .setLargeIcon(result)
            .setAutoCancel(true)
            .build();
like image 509
Pooja Pachchigar Avatar asked Jun 30 '17 11:06

Pooja Pachchigar


People also ask

How do you dismiss a notification?

To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.

How do I add an action button to my notifications?

Adding an action button: It is similar to setting the tap action by creating a pending intent only change is that we set it using addAction() to attach it to the notification. We can create intents for both activities or broadcast receivers as shown below: You can create your own BroadcastReciever.


1 Answers

Try like this

public void showNotification(Context ctx) {
    int noti_id = 100;
    NotificationManager notificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent actionIntent = new Intent(ctx, ActionReceiver.class);
    actionIntent.putExtra("noti_id", noti_id);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, noti_id, actionIntent, 
        PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification.Builder(ctx)
                                .setContentTitle("hello")
                                .setContentText("Hello")
                                .addAction(R.drawable.bell, "dismiss", pendingIntent)
                                .setAutoCancel(false)
                                .build();


    notificationManager.notify(noti_id, notification);   
}



public class ActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            int noti_id = intent.getIntExtra("noti_id", -1);

            if (noti_id > 0) {
                NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.cancel(noti_id);
            }
        }
    }
}
like image 53
VinayagaSundar Avatar answered Sep 28 '22 23:09

VinayagaSundar