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();
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.
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.
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);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With