【问题标题】:Android - Unable to Display Actions in Notification barAndroid - 无法在通知栏中显示操作
【发布时间】:2017-01-18 10:18:21
【问题描述】:

我正在开发 Android 中的可操作通知。

void createActionableNotification() {
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    // Build notification
    // Actions are just fake
    Notification noti = new NotificationCompat.Builder(this)
            .setContentTitle("New mail from " + "test@gmail.com")
            .setContentText("Subject").setSmallIcon(R.drawable.moneybag)
            .setContentIntent(pIntent)
            .addAction(R.drawable.moneybag, "Call", pIntent)
            .addAction(R.drawable.moneybag, "More", pIntent)
            .addAction(R.drawable.moneybag, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // hide the notification after its selected

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}

所以它显示像这样的操作,请检查下面的图片。

应用程序正在使用目标 SDK 运行,编译 SDK API 为 24,Min SDK 为 20。 我用 Marshmallow Device 进行了测试。但在模拟器中它工作正常。模拟器 SDK 版本为 24

【问题讨论】:

    标签: android android-notifications android-notification-bar


    【解决方案1】:

    试试这个:适用于 api >= 16

     Notification notification = new Notification.Builder(myContext)
    .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Subject").setSmallIcon(R.drawable.moneybag)
                .setContentIntent(pIntent)
                .setPriority(Notification.PRIORITY_MAX)
                .addAction(R.drawable.moneybag, "Call", pIntent)
                .addAction(R.drawable.moneybag, "More", pIntent)
                .addAction(R.drawable.moneybag, "And more", pIntent).build();
    
    //The rest of your options
    .build();
    

    当列表中有任何 Ongoing Notification 时,按钮不会出现,例如媒体播放器控件,或者您通过 USB 将手机连接到 PC 或在编辑文本时...

    你可以尝试Notification.PRIORITY_MAX 来解决这个问题,或者PRIORITY_HIGH

    【讨论】:

    • 感谢您的回答。我也尝试了 Priority 和 setwhen(0) 。但没有变化。我的手机中没有任何正在进行的通知@rafsanahmad007
    • 尝试在通知中添加样式:.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
    • 也可以尝试拔掉USB