How to Dismiss Notification After Action Has Been Clicked

How to dismiss notification after action has been clicked

When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:

notify(int id, Notification notification)

To cancel, you would call:

cancel(int id)

with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?

How to dismiss notification after action button clicked without opening the app?

Found the answer while surfing the GitHub Repository of awesome_notifications.
The notification can straightly be dismissed without opening the app by adding buttonType as ActionButtonType.DisabledAction in the NotificationActionButton

just like this:

NotificationActionButton(
key: 'DISMISS',
label: 'Dismiss',
autoDismissible: true,
buttonType: ActionButtonType.DisabledAction,
isDangerousOption: true
)

Note: Doing so will not trigger any receivedAction in the actionStream.

Dismiss current notification on Action clicked

I found it

You pendingIntent is always sending request code == 0;

Since you have multiple Notifications, each one should use a different requestCode.

So, try to change:

From:

PendingIntent pendingCancelIntent = 
PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

To:

PendingIntent pendingCancelIntent = 
PendingIntent.getBroadcast(context, this.notificationId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

I tested your code here and it's working after the change I did.

Clear notification on Action Button click

To close the notification when the "Sluiten" link is clicked on the notification I have included a working version of code.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

final int NOTIFICATION_ID = 1;
NotificationManager notificationManager;
Button notificationBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(getIntent()!=null && getIntent().hasExtra(getPackageName())){
notificationManager.cancel(NOTIFICATION_ID); //closes notification
}
notificationBtn = (Button) findViewById(R.id.notificationBtn);
notificationBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.notificationBtn:
showNotification();
break;
}
}

void showNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_account_box_black_24dp);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Geschiedenis \nFrans \nDuits \nNatuurkunde \nWiskunde"));
builder.setContentTitle("Rooster: ");
builder.setOngoing(true);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
Intent showIntent = new Intent(this, MainActivity.class);
showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Sluiten", contentIntent);
builder.setContentIntent(contentIntent);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}


}

NOTE
In the previous answer I did not include the line

showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()

which is essential to closing the notification on click

Dismiss Ongoing Android Notification Via Action Button Without Opening App

Start with this:

int final NOTIFICATION_ID = 1;

//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
buttonIntent.putExtra("notificationId",NOTIFICATION_ID);

//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);

//Pass this PendingIntent to addAction method of Intent Builder
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
.....
.....
.....
mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
manager.notify(NOTIFICATION_ID, mb.build());

Create the BroadcastReceiver:

public class ButtonReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

int notificationId = intent.getIntExtra("notificationId", 0);

// Do what you want were.
..............
..............

// if you want cancel notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}

If you don´t want show any activity when user click on notification, define the intent passed in setContentIntent in this way:

PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);
......
......
mb.setContentIntent(resultPendingIntent);

To close notification tray when clicked, call setAutoCancel() with true when building the notification: mb.setAutoCancel(true);

Removing notification after click

Use the flag Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);

// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

and to launch the app:

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

// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);

// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);


Related Topics



Leave a reply



Submit