Pooja Pachchigar
Pooja Pachchigar

Reputation: 227

How to dismiss notification on action click?

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();

Upvotes: 9

Views: 9558

Answers (3)

preqel
preqel

Reputation: 21

I had a same error.Finally I get the solution the problem is beacause the reqeust code you write is same when you have numbers of PendingIntent. It make the one PendingIntent work but the others doesn't work and the click is unuse. In my case ,just make the request code different ,and the problems solved.

Upvotes: 0

VinayagaSundar
VinayagaSundar

Reputation: 1701

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);
            }
        }
    }
}

Upvotes: 8

Paresh
Paresh

Reputation: 6857

You can cancel notification using notification_id you provided while creating notification using NotificationManager

To notify -

mNotificationManager.notify(_notificationId /* ID of notification */, mBuilder.build());

To cancel notification -

mNotificationManager.cancel(_notificationId);

Upvotes: -1

Related Questions