Can't dismiss notification by swiping - android

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.

Related

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

Notification sound in android

In my android application, I want to notify the user with a sound without showing the notification. Is that possible ?
Notification n = new Notification();
n.icon = com.android.mms.R.drawable.wap_message;
n.defaults |= Notification.DEFAULT_SOUND;
n.flags = Notification.FLAG_AUTO_CANCEL;
This is the code I am using to show the notification, So I commented icon and flags of the notification as follows
Notification n = new Notification();
//n.icon = com.android.mms.R.drawable.wap_message;
n.defaults |= Notification.DEFAULT_SOUND;
//n.flags = Notification.FLAG_AUTO_CANCEL;
But it is not working.
why don't you use MediaPlayer instead of Notification to play the notification sound. If you use the Notication then it shows the icon. In your case using the MediaPlayer is better option.

how to use NOTIFICATION_SERVICE in android

I want to show a notification after receiving a WAP push message in my phone.I know that NOTIFICATION_SERVICE is i need to be used. But i don't know how to use this. Please help me to overcome this problem. I am able to receive the message in my phone and it is printing in the log correctly. Written some code for showing the notification but it is not working. Attaching the code along with please have a look on it and expecting a proper guiding on this.
private void showNotification(String text,Context con) {
Notification n = new Notification();
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
n.icon = R.drawable.ic_sms_wap;
//n.when = System.currentTimeMillis();
// Simply open the parent activity
// Change the name of the notification here
//n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
mNotifMan.notify(NOTIF_CONNECTED, n);
}
hard to answer without knowing exactly what your problem is (ie do you get an error etc..) but you could try this:
private void showNotification(String text,Context con) {
if (mNotifMan==null) {
mNotifMan=(NotificationManager) con.getSystemService(NOTIFICATION_SERVICE);
}
Notification n = new Notification(R.drawable.ic_sms_wap,text,System.currentTimeMillis());
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
// n.icon = R.drawable.ic_sms_wap;
// n.when = System.currentTimeMillis();
// Simply open the parent activity
// Change the name of the notification here
//n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
mNotifMan.notify(null,NOTIF_CONNECTED, n);
}

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;

Categories

Resources