---
title: "Remote view in android : Auto Cancel Custom Notification"  
description: "Remote view in android : Auto Cancel Custom Notification"  
author: "Anonymous User"  
published: 2015-12-30  
updated: 2015-12-30  
canonical: https://www.mindstick.com/forum/33810/remote-view-in-android-auto-cancel-custom-notification  
category: "android"  
tags: ["android", "android controls"]  
reading_time: 2 minutes  

---

# Remote view in android : Auto Cancel Custom Notification

I am using [remote](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view) to create a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [layout](https://www.mindstick.com/articles/12853/android-layout-and-its-type) [notification](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) , i want to [auto](https://answers.mindstick.com/qa/111171/what-is-android-auto) [cancel](https://answers.mindstick.com/qa/94501/how-can-i-cancel-my-singapore-flight-ticket) the notification once the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) touches it \
MyCode:

```
RemoteViews remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);          NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);    builder.setSmallIcon(R.drawable.ic_launcher);    Intent intent = new Intent(MainActivity.this, SecondActivity.class);    PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, RQST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);    remoteView.setOnClickPendingIntent(R.id.btnNotification, pIntent);    builder.setAutoCancel(true);    builder.setContent(remoteView);     Notification notify = builder.build();    notify.flags |= Notification.FLAG_AUTO_CANCEL;    NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    notiManager.notify(NOTY_ID, notify);
```

## Replies

### Reply by Anonymous User

The way I achieved this is by creating BroadcastReceiver which controls button clicks from Notification. Create something like this :\

```
public class NotificationButtonListener extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        int action = intent.getIntExtra("mode", -1);        switch (action) {            // do some things depending on action if there are more stuff to do        }    }}
```

Don't forget to add your BroadcastListener to your manifest file:\

```
<receiver android:name=".NotificationButtonListener">
```

And you can create a helper class for creating and cancelling notification:

```
public class NotificationHelper {    private static final int NOTIFICATION_ID = 5;    private static NotificationManager mNotificationManager = null;    public static void showNotification(Context context) {        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);        contentView.setOnClickPendingIntent(R.id.btn_close, closePendingIntent);        Intent mMainIntent = new Intent(context, MainActivity.class);        mMainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);        PendingIntent mMainPendingIntent = PendingIntent.getActivity(context, 555, mMainIntent, PendingIntent.FLAG_UPDATE_CURRENT);        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)                .setTicker("Playing")                .setContent(contentView)                .setAutoCancel(false)                .setOngoing(true)                .setSmallIcon(R.drawable.ic_av_play)                .setContentIntent(mMainPendingIntent);        Notification notification = mBuilder.build();        mNotificationManager.notify(NOTIFICATION_ID, notification);    }    public static void cancelNotification() {        if (mNotificationManager != null) {            mNotificationManager.cancelAll();        }    }}
```

\
And using that class in your NotificationButtonListener you can call NotificationHelper.cancelNotification();


---

Original Source: https://www.mindstick.com/forum/33810/remote-view-in-android-auto-cancel-custom-notification

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
