Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close notification drop-down in Android >= 3.0 from custom rich notification with additional button?

I successfully created a custom rich notification for Android >= 3.0 that shows some text and an additional button. If you click the notification anywhere but on the button in the notification drop-down, the notification is dismissed, the drop-down closed and the specified Intent is launched as expected. If the dedicated button in the notification is clicked, a different Intent is successfully launched, but the drop-down keeps open (and the notification is still there, but I cancel it later, that is not the problem). The Intent launches an Activity which appears behind the notifications drop-down.

What I like to achieve is to keep all the current behavior as described, but also close the notification drop-down from the Intent the button launches - is this possible? Alternatively it would be enough if the Activity from the button Intent gains the window focus.

Here the code for the custom notification, if that helps:

            Notification.Builder builder = new Notification.Builder(main)
                .setSmallIcon(R.drawable.notification)
                .setAutoCancel(true)
                .setTicker(text)
                .setContentIntent(...);

            RemoteViews layout = new RemoteViews(
                    main.getPackageName(), R.layout.notification);
            layout.setTextViewText(R.id.title, title);
            layout.setTextViewText(R.id.text, text);
            Intent i = new Intent(
                    "snooze", null, main, Snooze.class
            );
            i.putExtra(KEY_WIDGET_ID, widgetId);
            layout.setOnClickPendingIntent(R.id.notification_zzz, PendingIntent.getActivity( main, 0, i, PendingIntent.FLAG_UPDATE_CURRENT ));
            builder.setContent(layout);

            ...
            NotificationManager nm = 
                (NotificationManager)main.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(0, builder.getNotification());
like image 914
mdiener Avatar asked Nov 04 '22 07:11

mdiener


1 Answers

Asked the question in the Android developer office hours: http://www.youtube.com/watch?v=XvLBvdml_Fs (question starting 49:10)

The answer was, that it is not possible and I should not even do this, which I question. That is why I have created a feature request: http://code.google.com/p/android/issues/detail?id=24762

EDIT 08-10-12: Starting with Android 4.1 this is possible as notifications can now include additional actions. See the following page for more information: https://developer.android.com/about/versions/android-4.1.html#UI

like image 101
mdiener Avatar answered Nov 14 '22 20:11

mdiener