10

Android's Google app has a Notification which shows the local weather for example. If you swipe left on the notification, it shows a "clock icon"-action (e.g. Snooze) as shown below.

Google Notification with Weather Forecast

If you click on the clock, the following dropdown menu opens:

Notification with dropdown menu

This is a feature of the android system, I want to implement in my app. It should be opened via a notification action, I want to set custom options, and I need to get the selected value.

Does anyone know how to implement that?

the_dani
  • 2,466
  • 2
  • 20
  • 46
  • This guy's answer will save you https://stackoverflow.com/a/21927248/6751183 – Vincent Mungai Sep 17 '18 at 23:58
  • Well i do not want do make a notification with a button, I do want to expand my normal android notification after an notification action was clicked! – the_dani Sep 18 '18 at 09:05
  • @VIGOPIXelInteractiveInc i do not want to make a notification with a costum content view, the notification should just expand on notification action click (a bit like the reply feature, where a textview appears in the notification... – the_dani Sep 18 '18 at 09:06
  • @the_dani you mean you want notification just like in a musicplayer notification with buttons ,text and images? – Kevin Kurien Oct 08 '18 at 06:57
  • @KevinKurien I've a normal notification with some actions.. When the user clicks on the 'Snooze' action button, the notification should expand, showing a RadioButton list with some possible snooze delays... I think I've seen that in a google app but I do not remember which one it was! – the_dani Oct 08 '18 at 07:44
  • Interesting question. I'm curious too – user1209216 Feb 12 '19 at 19:56
  • https://developer.android.com/training/notify-user/custom-notification use this link what you need is the big notification content – Pouya Danesh Feb 19 '19 at 13:48
  • @pouya But I need to call my custom layout after a Notification action was pressed.. If I use a expandable layout, this is not possible? – the_dani Feb 19 '19 at 14:19

2 Answers2

0

This is standard, for all the Notification from Android oreo.

For all the notification you get this option for snoozing. If you want to implement this same ui for older Android device, you can directly check the source code of Android oreo System UI, from where we get the snooze option in Android 8 and above.

http://androidxref.com/8.1.0_r33/xref/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationSnooze.java#274

Check the use of this string

<!-- Notification: Snooze panel: message indicating how long the notification was snoozed for. [CHAR LIMIT=100]-->
1520    <string name="snoozed_for_time">Snoozed for <xliff:g id="time_amount" example="15 minutes">%1$s</xliff:g></string>

http://androidxref.com/8.1.0_r33/xref/frameworks/base/packages/SystemUI/res/values/strings.xml#1519

nkalra0123
  • 2,281
  • 16
  • 17
  • Well, this is clear, but is there a way to make a notification(dialog) like shown above accessible via a notification action and retrieve its value? – the_dani Feb 16 '19 at 11:16
0

The expand feature is provided by the Android System for different notification styles. You need a view which displays a list of options that the user can pick from. So you need to create a Custom View and populate it with your options.

You can set the custom view using Notification.DecoratedCustomViewStyle() provided by Android Notification System.

If you want different appearances for collapsed and expanded view then you use the following methods to set them -

setStyle(new Notification.DecoratedCustomViewStyle())
   .setCustomContentView(remoteViews)
   .setCustomBigContentView(bigRemoteView);

You will need to add different pending intent for all the options you specify in your layout.

For example -

RemoteViews firstOption = ....;
Intent firstOptionIntent = // add some argument in this intent which depicts this option
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,   flags);
setOnClickPendingIntent(R.id.<option_layout_id>, pendingIntent);

// similary for other options

RemoteViews secondOption = ....;

To add a dropdown list on notification action click, you need to use 2 different layouts, one for the collapsed view and one for the expanded view -

Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(notificationLayout) // collapsed view layout
    .setCustomBigContentView(notificationLayoutExpanded) // expanded view layout
    .build();
Vishal Arora
  • 2,524
  • 2
  • 11
  • 14