Android 4: can't dismiss notification by swiping - android

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.

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.

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);

Just insert this line when you make the notification...
// Will show lights and make the notification disappear when the presses it
notification.flags == Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

Related

Can't dismiss notification by swiping

I have a Service that send notification to user acording to some conditions
private void sendNotif(String title, String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationService.this)
.setContentTitle(title)
.setContentText(msg)
.setSmallIcon(android.R.drawable.stat_notify_chat);
Notification notification = builder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
startForeground(DEFAULT_NOTIFICATION_ID, notification);
}
And now I have problems with dismissing this notification.
I try to use: .setOngoing(true);,
I try to use: notification.flags |= Notification.FLAG_ONGOING_EVENT;
Try to use NotificationManager, but dont find way how to startForeground using notification with NotificationManager.
I know that notification is alive while startForeground, so I try to add stopForeground to onDestroy(), but It still didn't help. I google tens of pages, but still can't find way to solve this problem. Will appreciate any help.

Android: How do I show 2 notifications in a row

The method bellow is being called twice with a different name parameter but only 1 notification appears on my device (the last one). I thought that putting an unique request mode parameter inside PendingIntent.getActivity() with name.hashCode() would work but that didn't solve the problem. So how can I alter this method to make my device show 2 notifications in a row instead of just the last one?
private void showNotification(String name, String sub) {
Intent intent = new Intent(activity.getApplicationContext(),
FragmentTabsPager.class);
PendingIntent pIntent = PendingIntent.getActivity(
activity.getApplicationContext(), name.hashCode(), intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
activity.getApplicationContext());
builder.setContentTitle("Hello world"
).setContentText(name+" from "+sub)
.setSmallIcon(R.drawable.icon).setContentIntent(pIntent)
.getNotification();
NotificationManager notificationManager = (NotificationManager) activity
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
Notification notification = builder.getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
The first parameter to notify() is the ID of the notification. That needs to be different if you want different Notifications to appear on the screen. If you call notify() twice with the same ID, the Notification is replaced, not added.

Android Notifications not disappearing (Even with Auto_Cancel)

I have different notifications that each have a different bundle/activity associated with them. My problem is that they don't disappear once clicked. They aren't under ongoing notifications though and "clear" gets rid of them. Bellow is my code. Any thoughts would be greatly appreciated. :)
private void showNotification(Bundle b){
CharSequence myText = b.getString("notifStr");
Notification notification = new Notification(R.drawable.stat_sample, myText,System.currentTimeMillis());
Intent i = new Intent(myContext, NewPlace.class);
i.setAction(Intent.ACTION_VIEW + Integer.toString(b.getInt("id")));
i.putExtras(b);
PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, i, 0);
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(myContext, myText,myText, contentIntent);
notifMan.notify(b.getInt("id"), notification);
}
try changing:
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
to
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Notification Documentation (Flags)
public int defaults
Since: API Level 1 Specifies which
values should be taken from the
defaults. To set, OR the desired from
DEFAULT_SOUND, DEFAULT_VIBRATE,
DEFAULT_LIGHTS. For all default
values, use DEFAULT_ALL.
You should try
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Android Notifications: use Email sound and Reminder sound instead of Default (text message?) sound

I am using the emulator so I don't know what sounds it makes, but according to one customer (not tech savvy), the sound it is making is the same as text messages.
Here is the code I am using
Notification notification = new Notification(R.drawable.stat_notify_email_generic, "New Email", timeInMillis);
if (soundAndVibrate)
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MyActivityFoo.class).putExtra(......
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
manager.notify(1, notification);
How would I change it to use the New Email sound? since it is in fact an email notification. Also I would like similar instructions to make the same sound as the Calendar Alarm/Reminder.

How can I enable vibration and lights using the Android notifications api?

I have created an application that creates notifications, using the following code:
// notification
Notification notification = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// parameters
String ringtone = prefs.getString(context.getString(R.string.key_notifications_ringtone), "");
if (ringtone.length() > 0) {
notification.sound = Uri.parse(ringtone);
notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
}
boolean useVibrator = prefs.getBoolean(context.getString(R.string.key_notifications_use_vibrator), false);
if (useVibrator) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
boolean useLed = prefs.getBoolean(context.getString(R.string.key_notifications_use_led), false);
if (useLed) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
// alert
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.icon);
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, text);
notification.contentView = contentView;
Intent notificationIntent = new Intent(context, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManager.notify(1, notification);
The notification works, and the correct ringtone is used.
However, even though the preferences are correctly activated and notification flags are correctly set (I checked by debugging), the notification never vibrates and never cause the lights to be activated.
I would have blamed my phone's settings, but every other app using notifications, like messaging, gmail, and others correctly use all these features.
May someone know what I did wrong ? (my phone is a HTC Hero with Android 2.1)
Add permission to your manifest file
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
EDIT
For Lights try adding them explicitly, the Default light might be configured to be nolight
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
In addition to Pentium10'S answer:
Put your device to sleep and the lights will go on! ;)

Categories

Resources