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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top