【问题标题】:Android Notification.Builder: show a notification without iconAndroid Notification.Builder:显示没有图标的通知
【发布时间】:2013-04-23 13:29:05
【问题描述】:
 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);

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

【问题讨论】:

  • 我知道的唯一方法是为图标使用透明图像,然后设置通知的顺序,以便您的通知排在最后(最右边),这将使用户看起来好像没有图标。
  • 为什么这符合用户的利益?
  • @CommonsWare :一个用例:一个隐身运行的通信应用程序确实希望用户知道消息何时到达,但不想显示应用程序图标或任何图标,以便其他人不能见。

标签: android android-notifications


【解决方案1】:

在 Jelly Bean 及以后您可以使用nbuilder.setPriority(Notification.PRIORITY_MIN);此优先级的确切解释由系统 UI 决定,但在 AOSP 中,这会导致通知图标被隐藏。

这适用于不需要引起用户注意的“环境”或“背景”信息,但如果用户碰巧在通知面板周围闲逛,她可能会感兴趣。有关如何最好地使用通知优先级的更多信息,请参阅Notifications section of the Android Design site

【讨论】:

    【解决方案2】:

    更新:不要在 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.contentView != 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);
      }
    }
    

    我找不到更好的方法。

    当他们更改视图的 id 或更改 Notification 中的任何 RemoteViews 字段时,这可能会停止工作,因此使用风险自负。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        您可以设置自己的无图标布局:

        RemoteViews notificationView = new RemoteViews( context.getPackageName(), R.layout.notify ); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContent(notificationView)

        【讨论】:

          【解决方案5】:

          尝试在您的通知生成器中添加此行
          .setPriority(Notification.PRIORITY_MIN)
          所以你的通知会是这样的:
          Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.stat_notify_chat) .setContentTitle("my app") .setContentText("my app ") .setPriority(Notification.PRIORITY_MIN) .setContentIntent(pendingIntent).build();
          因此,在这种情况下,您可以在 Notification bar 中看到您的图标,它会隐藏在 status bar 中。希望对您有所帮助。

          【讨论】:

            【解决方案6】:

            我之前遇到过这个问题,然后就这样解决了:

            private int getNotificationIcon() {
                boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP);
                // LOLLIPOP or Marshmellew>>>>>>>>>>>>>>>>>>>>> KitKat or Less
                return useWhiteIcon ? R.drawable.logo_new : R.drawable.logo;
            }
            

            然后在setSmallIcon()中调用这个函数

            nbuilder.setSmallIcon(getNotificationIcon());
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-02-20
              • 2020-08-19
              • 1970-01-01
              • 2017-03-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多