Android通知按钮未显示

44

这是我的代码,用于设置带有按钮的通知。

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版本。我在一个Fragment中设置了这个通知。我做错了什么?谢谢!

9个回答

133

让我告诉你一些非常尴尬的事情。 如果您的正在进行通知中有任何内容,您将无法看到按钮。 通常情况下,当您的手机通过 USB 连接到电脑时会出现这种情况。 希望这可以解决您的问题。


8
今年的答案是......我责怪我最近更新的操作系统(4.2.2),但它并不是直接的原因。 - Alexandru Circus
18
很好的答案。我也通过使用@Captain Blammo的答案来解决这个问题,即在我的通知构建器上调用.setPriority(Notification.PRIORITY_MAX)。这样,即使您通过USB连接,仍然可以看到按钮。 - Jake
14
当您的通知周围还有其他通知时,您可以向下滑动通知以展开它并显示按钮。 :) - Erlend D.
2
如果你的通知里有任何东西,这是什么意思? - frankelot
2
"ANYTHING IN YOUR NOTIFICATION" 意味着任何其他通知,即任何事情 :-) - CommonMan
显示剩余3条评论

31

提醒大家一下,如果有类似问题的人可以参考 Android 通知指南。通知可以呈现为两种类型:

  • 普通视图,动作按钮默认情况下不会显示(用户必须展开通知才能看到)
  • 大视图,在通知列表中第一条通知或用户展开通知时可见。

因此,为了强制通知以大视图形式出现,我们只需将其放置在通知列表的顶部。这可以通过将“when”属性设置为0来实现,这样它就成为最旧的通知之一!(但有时我们可能不希望这样做)。所以调用

setWhen(0)

发送通知后,您就完成了。


7
甚至更好的是将通知的优先级设置为最高!setPriority(Notification.PRIORITY_MAX) - paulahniuk
2
你应该把那个作为答案添加进去!愿上帝保佑你! - Swayam
3
жҲ‘жңҖз»ҲдёҚеҫ—дёҚеҗҢж—¶дҪҝз”ЁsetWhen(0)е’ҢsetPriority(MAX)жүҚиғҪдҪҝе…¶еңЁAndroid 4.4дёҠжӯЈеёёе·ҘдҪңгҖӮ - Calin

26

如果通知栏中有进行中的通知,例如媒体播放器控制或编辑文本时的IME切换选项,按钮将不会出现。

幸运的是,这可以通过将通知的优先级设置得非常高来解决。我只用过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();

1
优先级设置现已弃用。是否有更新的解决方案? - szidijani
有新的解决方案吗?因为它已经过时了。 - DIRTY DAVE
我们可以使用setPriority(NotificationCompat.PRIORITY_HIGH)代替setPriority(Notification.PRIORITY_MAX)。 - Thirumalvalavan

18

只需这样做:::

.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();

1
如果您想知道为什么“按钮”没有显示出分配的图标(只显示文本-甚至不是按钮,没有可见的边框),那可能是因为安卓决定放弃显示图标,而在addAction方法中没有记录这一弃用情况。至少,在sdk-28(安卓10)上,以上任何已接受的答案(从7年前开始)都没有让我看到任何图标-所以我认为这必须是一个未经记录的设计决策,直到有证据证明否则 :)

0

我在NotificationCompat.Builder中遇到了这个问题,我已经完成了以下步骤:

添加以下行:

.setPriority(NotificationCompat.PRIORITY_HIGH)

.setWhen(0)

以下行已删除:

.setContentIntent(pendingIntent)

在这些更改之后,按钮将显示在通知中。


0
在我的情况下,我添加了一个持久通知。这意味着如果你有一个持久通知显示,那么操作将不会显示。

0
在我的情况下,操作按钮没有显示出来,因为我正在使用自定义视图来处理通知内容,使用了RemoteViews()。

-1

只需在您的代码中进行一些更改即可

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 Impresspeed E200和三星Galaxy Note Tab 10.1上都发生过,两者都是JB系统。这真是太疯狂了,浪费了半天时间!还有其他人遇到这个问题吗? - DraganescuValentin
在几乎所有三星安卓设备中都存在这个bug。因此,你不是唯一面临这个问题的人。像往常一样,三星用它的TouchWiz垃圾破坏了安卓的功能!... - ChuongPham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接