1

i am trying to show a notification with more than 3 actions. Unfortunately, the forth action and so on are not showing (probably because there not enough space). Also, the action items does not have the same width.

enter image description here

Does anyone know how can i display more than 3 actions?

This is my code:

final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(message.getData().get(DATA_TITLE));
    inboxStyle.addLine(message.getData().get(DATA_BODY));

    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentTitle(message.getData().get(DATA_TITLE))
            .setContentText(message.getData().get(DATA_BODY))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setStyle(inboxStyle)
            .setContentIntent(defaultIntent)
            .setPriority(Notification.PRIORITY_MAX);

    addActions(notificationBuilder, message);

    private void addActions(final NotificationCompat.Builder builder, final RemoteMessage message) {
    if (containsAction(message, EventActionType.OpenMessage)) {
        builder.addAction(R.drawable.ic_email, "open", getActionIntent(message, MyActivity.class));
    }
    if (containsAction(message, EventActionType.Details)) {
        builder.addAction(R.drawable.ic_notification_account, "details", getActionIntent(message, MyActivity.class));
    }
    if (containsAction(message, EventActionType.Transfer)) {
        builder.addAction(R.drawable.ic_access_time, "transfer", getActionIntent(message, MyActivity.class));
    }
mspapant
  • 1,860
  • 1
  • 22
  • 31
  • 2
    I think having more than two actions is just going to make it cluttered and confusing. You're better off having one primary action and then a "more" button or something that opens an activity to do other things. – Matti Virkkunen Jul 03 '16 at 14:37
  • Like Matti says, Notifications are meant to notify, not to do complex CRUD actions. You have very little space by design but android 5+ iirc has more types of standard notifications. For these types of problems it's often best to just not fight the system and use the standard messages. Good luck! – G_V Jul 03 '16 at 15:45
  • Thank you for your comments, but i have to use more actions than a primary one, this is a business requirement. Does anyone know how to display to achieve this? – mspapant Jul 04 '16 at 09:37

1 Answers1

4

This link can help you. According to standard docs of android we can't have more than three actions for a notification.
If you want to have more than three actions , you can use remoteViews. Every notification will have a default view which is provided by android os, but we can customise it. To do that we need to create a layout will be used as a our notification view and use as many as buttons as you want in that layout, but height of the layout is limited as notification height is limited by android.Make sure all your buttons will fit in notification.Create a remoteViews with this layout.

After that when you are creating notification attach it to notification using setCustomContentView. For each button in the view you can have different pendingintent i.e on clicking on different buttons different pending intents can be executed. see RemoteView.setOnClickPendingIntent(view,pendingIntent).

Happy coding ;-)

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Ajay Naredla
  • 408
  • 5
  • 17