Pushnotification not removed from notificationbar in android - android

In my application i received push notification from gcm, if i click on that push notification, my application will open and i need to remove my notification from notification bar. But , it is still on notification bar.
I used the following logic for generating push notification in my GCMIntentService class.
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
//PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TipsActivity.class), 0);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TipsActivity.class),
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Appname")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
So, please guide me how to remove my notification , when i click on it.

Hi You have to add the following line>..
mBuilder.setAutoCancel(true);
Try this and let me know the feedback.

as per my guessing it is happening due to flag of notification
may be there is line in your code related to notification flag
may be that flag is FLAG_NO_CLEAR so change that flag to FLAG_AUTO_CANCEL
you will find this flag in generateNotification logic if you not mentioned the flag then please specify it as FLAG_AUTO_CANCEL
FLAG_NO_CLEAR means
Bit to be bitwise-ored into the flags field that should be set if the notification should not be canceled when the user clicks the Clear all button
and
FLAG_AUTO_CANCEL means
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
here is the sample line
notification.flags |= Notification.FLAG_AUTO_CANCEL;
where notification is object of NotificationManager
use this link for refference
hope it will resolve your issue happy coding

Related

How to receive notification when the application is closed?

I'm making an android application, and I want to receive notifications when the application is closed, how can I do that? I've read also other posts where people ask it but i can't understand how to make it work, could you please try to explain this better?
I've also tried to make a countdownTimer and make the application do something when the timer reaches the 0 but it didn't work, 'cause obviusly when I close the application, I close the timer too.
use this code to make in app notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());

New notification should not cancel the previous notification in android

Thanks for viewing. I don't want to replace previous notification with new, when notification is fired. In my case, notification are getting replaced. Notifications should be cleared only, if user presses android built-in clear notifications option. Thanks for your help mates.
Here it is, what i've tried:
Intent notifyIntent =
new Intent(new Intent(this, Dashboard_DrawerMain.class));
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
contentIntent = PendingIntent.getActivity(this, 0,notifyIntent
, 0);
mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(str_SenderName)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentText(str_Message)
.setAutoCancel(true);
mBuilder.setSmallIcon(R.drawable.ft_icon);
mBuilder.setContentIntent(contentIntent);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Please use differnt NOTIFICATION_ID on NotificationManager.notify
If the NOTIFICATION_ID is unique then the system will consider it as a new notification
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
But it is better to replace the notification if it is just an update to previous notification
Create the new Notification with a new ID and it won't clear the previous one.

What is the difference between Notification and NotificationManger in Android?

What is the difference between those two?
I want to use the startForeground method and cannot use it with NotificationManager..
Thanks
A Notification is a class that represents either a persistent icon that goes in the status bar and is accessible through the launcher, turning on or flashing LEDs on the device, or alerting the user by flashing the backlight, playing a sound, or vibrating.
The Notification Manager is the class that allows you to add notifications to the system,
startForeground is a method of the Serivce class. For example inside your Service class you can have something like this.
Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_play)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker(getString(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(someText);
Notification notification = builder.build();
startForeground(1, notification);
A Notification is a description of what you want to occur to alert the user about something -- what icon goes in the status bar, what ringtone to play, etc.
NotificationManager is a system service that can show a Notification.
I want to use the startForeground methode and cannot use it with NotificationManager
Correct. Create a Notification using Notification.Builder (or NotificationCompat.Builder). See this project for an example of using startForeground().

Android how to compose notification on notification bar

I am new to android. I want to now how to create a notification in the notification bar and compose it like 'usb connected' or 'usb debugging connected' notify. Can anyone help me with some codes?
I have used NotificationCompat.Builder for the purpose
First we have to style the notification
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // icon to be displayed
.setContentTitle("Notification") // title for the notification
.setContentText(msg); // msg is some text you want to display
For Led light blinkering when notification is received
mBuilder.setLights(Color.BLUE,1000,1200);
To give notification sound you can make use of default notification sound set by the user
Uri sound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(sound);
To cancel the notification when the user has selected it
mBuilder.setAutoCancel(true);
To start an activity in app when the user clicks the notification
Intent resultIntent = new Intent(this,ExampleActivity.class);
PendingIntent resultPendingIntent =PendingIntent.getActivity(this,0, resultIntent,0);
mBuilder.setContentIntent(resultPendingIntent);
Finally to show the notification in the bar make use of notification manager
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

clearing ongoing notification in android

In my application iam showing a ongoing notification while doing a video upload. I want to clear the notification after upload. I searched a lot and cant get a clear answer.Please help me if anybody knows...
From Managing your Notifications in android documentation:
You can also clear it manually with cancel(int), passing it the
notification ID, or clear all your notifications with cancelAll().
So you should perform following to hide the notification:
mNotificationManager.cancel(notification_id);
where mNotificationManager is NotificationManager object and notification_id is int identifier of notification passed to NotificationManager object:
mNotificationManager.notify(notification_id, notification);
Just to show how this is done with Notification.Builder you can set the FLAG_AUTO_CANCEL like this:
Notification.Builder mBuilder = new Notification.Builder(getContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle("My notification")
.setContentText("Hello Notify!")
.setOngoing(true)
.setAutoCancel(true);
You can clear your notification by clicking on it..!!
Notification notification = new Notification("Your needed data..");
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Categories

Resources