Android - 无法在通知栏中显示操作

时间:2017-01-18 10:18:22

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

我正在Android中开发Actionable Notifications。

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

所以它显示了这样的动作,请检查下面的图像。 enter image description here

该应用程序使用目标SDK运行,Compile SDK API为24,Min SDK为20。 我用Marshmallow Device进行了测试。但在模拟器中工作正常。仿真器SDK版本为24 enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:适用于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();

如果列表中存在任何正在进行的Notification(例如媒体播放器控件),或者您通过USB将电话连接到PC或正在编辑文本时,按钮将不会出现。

您可以尝试Notification.PRIORITY_MAX来解决此问题,或PRIORITY_HIGH