I do not want Notification Manager to open an activity

1.5k Views Asked by At

I do not want Notification Manager (Status bar) to open an activity. Instead when a notification is clicked, it is just cancelled, and nothing must happen.

3

There are 3 best solutions below

1
On BEST ANSWER
final NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, null);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

Rather than passing class in intent just pass null or use intent() default constructor instead . if helps appreciate.

0
On

Create an empty activity that calls finish() from onCreate() and set it in the PendingIntent. An empty (null) one is not allowed, so that is your best bet, AFAIK.

0
On

When you create the PendingIntent pass an empty Intent new Intent()

PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)

and set the FLAG_AUTO_CANCEL Flag for the notification

notification.flags |= Notification.FLAG_AUTO_CANCEL;