Android Notification.Builder:显示没有图标的通知

use*_*138 13 android android-notifications

 String ns = Context.NOTIFICATION_SERVICE;
 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

 int icon = R.drawable.ic_notification_icon;
 android.app.Notification.Builder nbuilder = new Notification.Builder(this);

 nbuilder.setContentTitle(getString(R.string.notifcation_title,mProfile.mName));
 nbuilder.setContentText(msg);
 nbuilder.setOnlyAlertOnce(true);
 nbuilder.setOngoing(true);
 nbuilder.setSmallIcon(icon,level.level);
Run Code Online (Sandbox Code Playgroud)

如何隐藏或完全删除smallIcon?我试图不使用nbuilder.setSmallIcon,但结果是通知根本没有显示!

dsa*_*ler 17

在果冻豆和以后你可以使用nbuilder.setPriority(Notification.PRIORITY_MIN); 此优先级的确切解释由系统UI决定,但在AOSP中,这会导致隐藏通知的图标.

这适用于不需要引起用户注意的"环境"或"背景"信息,但如果用户恰好在通知面板上戳,她可能会感兴趣.有关如何最好地使用通知优先级的更多信息,请参阅Android设计网站的" 通知"部分.


Mar*_*ues 9

更新:不要在Android 7上使用它,它正在崩溃


使用反射,这里有一些在Lollipop(模拟器和Moto G设备)和Marshmallow(模拟器)中工作的东西

Notification notification = NotificationCompat.Builder.blabla().build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  int smallIconViewId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());

  if (smallIconViewId != 0) {
    if (notification.contentIntent != null)
      notification.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

    if (notification.headsUpContentView != null)
      notification.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

    if (notification.bigContentView != null)
      notification.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
  }
}
Run Code Online (Sandbox Code Playgroud)

我找不到更好的方法.

这可以在他们更改视图的ID或更改通知中的任何RemoteViews字段时停止工作,因此使用风险自负.


Dha*_*mar 3

你不能这样做...没有setSmallIcon(icon,level.level);通知不显示..