I do not want Notification Manager to open an activity
-
11-06-2021 - |
Вопрос
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.
Решение
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.
Другие советы
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;
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.
Не связан с StackOverflow