Android Notification按钮未显示

Dra*_*tin 38 notifications android action button

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

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);
Run Code Online (Sandbox Code Playgroud)

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

Com*_*Man 126

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

  • 很好的答案.我也通过在我的表达构建器上调用.setPriority(Notification.PRIORITY_MAX)来使用@Captain Blammo来解决这个问题.这样,即使通过USB连接,您仍然可以看到按钮 (18认同)
  • 您可以随时向下滑动通知以展开它并显示按钮,即使周围有其他通知也是如此.:) (14认同)
  • 今年的答案....我指责我所做的最后一次操作系统更新(4.2.2),但不是它的直接错误.... (8认同)
  • 这个答案值得更多的赞成! (2认同)
  • 如果您的通知中有任何内容?那有什么意思? (2认同)
  • “您的通知中的任何内容”是指任何其他通知,即任何内容:-) (2认同)

pau*_*iuk 29

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

  • 普通视图,默认情况下不显示操作按钮(并且用户必须展开通知,以便显示它们)
  • Big View,如果通知是通知列表中的第一个,或者用户已扩展通知,则可见.

因此,为了强制通知显示在Big View中,我们所要做的就是将其放在通知列表的顶部.这可以通过将When属性设置为0来完成,这使其成为通知中最早的属性!(有时虽然我们可能不想要这个).所以打电话

setWhen(0)
Run Code Online (Sandbox Code Playgroud)

通知你已经完成了.

  • 或者甚至更好地将通知的优先级设置为MAX!setPriority(Notification.PRIORITY_MAX) (7认同)
  • 我最终不得不把'setWhen(0)`和`setPriority(MAX)`放在android 4.4上 (3认同)
  • 您应该添加它作为答案!愿好主保佑你! (2认同)

Cap*_*mmo 23

当列表中存在任何正在进行的通知时,按钮将不会出现,例如媒体播放器控件或编辑文本时的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();
Run Code Online (Sandbox Code Playgroud)


小智 16

做这个:::

.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
Run Code Online (Sandbox Code Playgroud)

完整代码是:

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();
Run Code Online (Sandbox Code Playgroud)