5

This is the code that gives notification on start of service

NotificationCompat.Builder mbuild = new NotificationCompat.Builder(getApplicationContext());

Intent in = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent resultIN =   PendingIntent.getActivity(getApplicationContext(),code,in,NOTIFICATION_COUNT);           mbuild.setSmallIcon(R.drawable.images1);    
mbuild.setContentText(NOTIFICATION_COUNT +" New Message");
mbuild.setContentIntent(resultIN);          //mbuild.addAction(R.drawable.notifications,NOTIFICATION_COUNT +"New Messages",resultIN);

 NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nmagr.notify(1, mbuild.build());

everyting is working correct ..the code opens the target activity but the notification still stays there in the notification bar. i have tried useing mbuil.setautocancel(true); but its doing nothing

Deepti
  • 119
  • 4
  • 12

4 Answers4

9

try this

NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification notification=mbuild.build();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            nmagr.notify(1,notification);
Santhosh
  • 1,867
  • 2
  • 16
  • 23
2

You didn't set setAutoCancel(true)

Just set this

mbuild.setAutoCancel(true)

or

mbuild.getNotification().flags |= Notification.FLAG_AUTO_CANCEL

Updated

You can also try below code.

NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.cancel(1); // here is "1" is your notification id which you set at "nmagr.notify(1, mbuild.build());"

write above code in your onCreate() method of MainActivity.class.

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1
NotificationManager notification_manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                                notification_manager.cancel(NOTIFICATION_ID);
Chetak Bhimani
  • 459
  • 4
  • 19
  • if i use notification_manager.cancel(id) jst below ma code i.e after nmagr.notify(1, mbuild.build()); it cancels the notification by itself before leting the notification cheked or tapped – Deepti Mar 06 '14 at 08:57
0

Just an update to anyone doing this with NotificationCompat and / or using the Notificaitoncompat.Builder.

This is how I did mine :

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentTitle(title);

/* your own code here */

builder.setAutoCancel(true); 

Notification notification = builder.build();
NotificationManagerCompat.from(this).notify(0,notification);

It is important to note that if you use a pending intent to redirect the user to a specific intent in your app, this will also call the pending intent.

As per the documentation :

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent will be broadcast when the notification is canceled.

Janpan
  • 2,164
  • 3
  • 27
  • 53