【问题标题】:Android Notification buttons not showing upAndroid 通知按钮未显示
【发布时间】:2013-08-17 11:25:50
【问题描述】:

这是我用按钮设置通知的代码。

Intent receiverIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        PendingIntent pReceiverIntent = PendingIntent.getActivity(ctx, 1, receiverIntent, 0);
        Intent clearIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        clearIntent.setAction("clear");
        PendingIntent pClearIntent = PendingIntent.getActivity(ctx, 1, clearIntent, 0);

        Intent colorsIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        colorsIntent.setAction("colors");
        PendingIntent pColorsIntent = PendingIntent.getActivity(ctx, 1, colorsIntent, 0);

        Intent animationIntent = new Intent(ctx, ResponsivePrefsActivity.class);
        animationIntent.setAction("animation");
        PendingIntent pAnimation = PendingIntent.getActivity(ctx, 1, animationIntent, 0);

        Notification.Builder builder;
        builder = new Notification.Builder(ctx).setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false)
                .setContentTitle("Draw Me: A Live Wallpaper").setContentText("Never get bored again!")
                .setContentIntent(pReceiverIntent).addAction(R.raw.ic_menu_close_clear_cancel, "Clear", pClearIntent)
                .addAction(R.raw.ic_menu_edit, "Colors", pColorsIntent).addAction(R.raw.ic_menu_play_clip, "Animation", pAnimation);
        Notification notification = builder.build();

        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification);

通知正在显示,但按钮不显示。我的设备有 Android 4.1.1 我在片段中设置了这个通知。我究竟做错了什么?谢谢!

【问题讨论】:

    标签: android button notifications action


    【解决方案1】:

    让我告诉你一些非常尴尬的事情。 如果您的持续通知中有任何内容,您将看不到按钮。 通常,当您通过 USB 将手机连接到 PC 时,就会发生这种情况。 希望这能解决你的问题

    【讨论】:

    • 年度最佳答案....我将责任归咎于我所做的最后一次操作系统更新 (4.2.2),但这不是它的直接错误....
    • 很好的答案。我还通过在我的通知生成器上调用 .setPriority(Notification.PRIORITY_MAX) 使用@Captain Blammo 回答解决了这个问题。这样,即使通过 USB 连接,您仍然可以看到按钮
    • 即使周围有其他通知,您也可以随时在通知上向下滑动以展开并显示按钮。 :)
    • 如果您的通知中有任何内容?这甚至意味着什么?
    • “您的通知中的任何内容”是指任何其他通知,即任何内容 :-)
    【解决方案2】:

    只是提醒任何有类似问题的人。 根据 Android Notifications Guide,通知可以以两种样式显示:

    • 普通视图,默认情况下不显示操作按钮(用户必须展开通知才能显示)
    • 大视图,如果通知在通知列表中排在第一位,或者用户已展开通知,则该视图可见。

    因此,为了强制通知出现在大视图中,我们只需将其放在通知列表的顶部即可。这可以通过将 When 属性设置为 0 来完成,这使它成为通知中最旧的! (有时我们可能不想要这个)。所以调用

    setWhen(0)
    

    到您的通知,您就完成了。

    【讨论】:

    • 或者最好将通知的优先级设置为 MAX! setPriority(Notification.PRIORITY_MAX)
    • 您应该添加它作为答案!愿上帝保佑你!
    • 我最终不得不将setWhen(0)setPriority(MAX) 都放在android 4.4 上进行这项工作
    【解决方案3】:

    当列表中存在任何正在进行的通知时,这些按钮不会出现,例如媒体播放器控件,或编辑文本时的 IME 切换器选项。

    幸运的是,这可以通过将通知的优先级设置为 nice 和 high 来克服。我只使用过 Notification.PRIORITY_MAX 来解决这个问题,但 PRIORITY_HIGH 似乎也可以。设置如下:

    Notification notification = new Notification.Builder(myContext)
    .setContentTitle(res.getString(R.string.my_title))
    .setPriority(Notification.PRIORITY_MAX)
    //The rest of your options
    .build();
    

    【讨论】:

    • 优先级设置现已弃用。有更新的解决方案吗?
    • 有什么新的解决方案吗?导致其弃用
    【解决方案4】:

    就这样做 :::

    .setPriority(Notification.PRIORITY_MAX)
    .setWhen(0)
    

    完整代码是:

    Notification noti = new Notification.Builder(this)
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setPriority(Notification.PRIORITY_MAX)
                .setWhen(0)
                .addAction(R.drawable.ic_launcher, "Call", pIntent)
                .addAction(R.drawable.ic_launcher, "More", pIntent)
                .addAction(R.drawable.ic_launcher, "And more", pIntent).build();
    

    【讨论】:

      【解决方案5】:

      如果您想知道为什么“按钮”没有显示指定的图标(仅显示文本 - 甚至不显示为按钮,没有可见的边框 - 我猜材料设计这么说)这可能是因为 android 决定放弃显示图标,而不是在 addAction-Method 的任何地方记录此弃用。

      至少上面接受的答案(从 7 年前开始)都没有让我在 sdk-28(android 10)上出现任何图标 - 所以我认为它必须是一个无证的设计决定,除非另有证明:)

      【讨论】:

        【解决方案6】:

        在我的例子中,操作按钮没有显示,因为我使用 RemoteViews() 的通知内容的自定义视图

        【讨论】:

          【解决方案7】:

          就我而言,这是我添加的持久通知。如果您显示持久通知,则表示操作不会显示。

          【讨论】:

            【解决方案8】:

            只需在您的 code 中进行一些类似的更改

            Notification n = new Notification.Builder(this)
                            .setSmallIcon(R.drawable.icon)
                            .setContentTitle("New mail from " + "test@gmail.com")
                            .setContentText("Subject")
                            .setContentIntent(pIntent).setAutoCancel(true)
                            .setStyle(new Notification.BigTextStyle().bigText(longText))
                            .build();
            
                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    // Hide the notification after its selected
            
                    notificationManager.notify(0, n);
            

            在此添加您需要的内容..希望对您有所帮助..

            【讨论】:

            • 我已经有了它可以看到的。我不需要 setStyle 方法,因为我想要小按钮。我学到的是,当平板电脑通过 USB 连接到电脑时,按钮没有显示。当我断开平板电脑时,按钮出现了。这已经在 E-BODA Impressspeed E200 和三星 Galaxy Note Tab 10.1 上发生,两者都带有 JB。这简直是​​疯了,浪费了半天!其他人有这个问题吗?
            • 这是几乎所有三星 Android 设备中的错误。所以,你不是唯一一个面临这个问题的人。像往常一样,三星用它的 TouchWiz 废话打破了 Android 功能!...
            猜你喜欢
            • 1970-01-01
            • 2013-04-09
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多