Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4: can't dismiss notification by swiping

I have some code that creates some notifications, it's really basic.

int icon = R.drawable.notification;
CharSequence tickerText = "Text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "Text";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, RequestActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

mNotificationManager.notify(notificationID, notification);

It all works fine in 2.1. In 4.0, it all works fine except the swipe-to-dismiss action doesn't work. The notification goes slightly to the side then sticks and bounces back. Any idea? Thanks.

like image 648
James Avatar asked May 23 '12 10:05

James


2 Answers

You should use setOngoing (boolean ongoing)

Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. They are typically used to indicate a background task that the user is actively engaged with (e.g., playing music) or is pending in some way and therefore occupying the device (e.g., a file download, sync operation, active network connection).

You can use

.setOngoing(false);
like image 113
IntelliJ Amiya Avatar answered Oct 21 '22 22:10

IntelliJ Amiya


You can't swipe away your notification, because it is in an "ONGOING"-State.

First the solution:

Replace setting flags with following code:

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Defaults are for the defaults-section, flags for the flags-section.

And now the reason why it was ongoing?

As you might already know flags (and defaults) for notifications are set by a bitwise operation. Means each flag has a constant value which is a power of 2. Adding them results in an unique number for a set of flags which makes it real fast to calculate which flags are actually set.

Notification.DEFAULT_VIBRATE and Notification.FLAG_ONGOING_EVENT have the same contant value of 2.

like image 24
AndacAydin Avatar answered Oct 21 '22 22:10

AndacAydin