【问题标题】:Android Notification with many actions are not shown未显示具有许多操作的 Android 通知
【发布时间】:2019-03-20 16:50:03
【问题描述】:

我正在尝试显示包含 3 个以上操作的通知。不幸的是,第四个动作等没有显示(可能是因为没有足够的空间)。此外,操作项的宽度也不相同。

有谁知道我怎样才能显示超过 3 个动作?

这是我的代码:

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));
    }

【问题讨论】:

  • 我认为有两个以上的动作只会让它变得混乱和混乱。您最好先执行一个主要操作,然后再添加一个“更多”按钮或打开 Activity 以执行其他操作的按钮。
  • 就像 Matti 所说的,通知的目的是通知,而不是执行复杂的 CRUD 操作。您的设计空间非常小,但 android 5+ iirc 有更多类型的标准通知。对于这些类型的问题,通常最好不要与系统对抗并使用标准消息。祝你好运!
  • 感谢您的 cmets,但我必须使用比主要操作更多的操作,这是业务需求。有谁知道如何显示来实现这一点?

标签: android android-notifications


【解决方案1】:

这个link 可以帮助你。 根据 android 的标准文档,我们的通知操作不能超过三个。
如果您想拥有三个以上的操作,可以使用 remoteViews。每个通知都会有一个由 android os 提供的默认视图,但我们可以自定义它。为此,我们需要创建一个布局,该布局将用作我们的通知视图,并在该布局中使用任意数量的按钮,但布局的高度受到限制,因为通知高度受 android 的限制。确保所有按钮将适合通知。使用此布局创建 remoteViews

之后,当您创建通知时,使用 setCustomContentView 将其附加到通知。对于视图中的每个按钮,您可以有不同的挂起意图,即单击不同的按钮可以执行不同的挂起意图。见RemoteView.setOnClickPendingIntent(view,pendingIntent)

快乐编码;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2023-03-10
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多