5

My notification contains several buttons :

  • 1 button launch back the main activity (should close status bar when doing so)
  • 4 of them send pending intents to control the music (should keep status bar open)

The problem is, the first button does not close the status bar...

the PendingIntent sent by the first button :

Intent activityIntent = new Intent(smp, MusicShaker.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activityIntent.setAction(MyIntentAction.DO_LAUNCH_ACTIVITY_FROM_NOTIF);
remoteViews.setOnClickPendingIntent(R.id.notif_album_IV, PendingIntent
            .getActivity(ctxt, 0, activityIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT));

the activity is correctly launched, but the status bar stays there and does not close itself.
Am I missing/misunderstanding a flag ? can I close the status bar progamaticaly from MyActivity.onResume() ?
edit: by the way, the notification is pushed by a service

thanks =)

elgui
  • 3,033
  • 4
  • 26
  • 37
  • Possible duplicate of [Clicking Android Notification Actions does not close Notification drawer](https://stackoverflow.com/questions/18261969/clicking-android-notification-actions-does-not-close-notification-drawer) – Michael Litvin Nov 08 '18 at 09:29

4 Answers4

3

You, just need to specify setAutoCancel(true) while creating builder. And that is all :)

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("title goes here")
            .setContentText("content text goes here").setAutoCancel(true);
rashadex
  • 31
  • 2
2

Yeah, you'll have to cancel the notification programmatically when your app starts.

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
devnate
  • 746
  • 5
  • 8
  • this solution allows me to remove the notification without closing the status bar, and I actually need the opposite behavior (closing status bar without removing the notif). – elgui Aug 22 '12 at 10:32
2

ok, I found a solution... I could not reproduce the same behavior produced by standard notification, so I :
- made my imageButton "notif_album_IV" non clickable, and change it to an ImageView
- used this code :

builder.setContentIntent(PendingIntent.getActivity(smp, 0,
  activityIntent,PendingIntent.FLAG_CANCEL_CURRENT))

instead of setting the setOnClickPendingIntent "manually" on the image, the intent broadcast is handled by the content background

elgui
  • 3,033
  • 4
  • 26
  • 37
1

This worked for me:

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Michael Litvin
  • 3,444
  • 1
  • 27
  • 35