How to create a notification without icon in the statusbar or simply hide it?

帅比萌擦擦* 提交于 2019-11-30 16:45:47

问题


I'd like to know how to create a notification that doesn't show the icon in the statusbar.

There is a way to hide it?


回答1:


Since Android 4.1 (API level 16) it's possible to specify a notification's priority. If you set that flag to PRIORITY_MIN the notification icon won't show up on the statusbar.

notification.priority = Notification.PRIORITY_MIN;

Or in case you use a Notification.Builder:

builder.setPriority(Notification.PRIORITY_MIN);

As of Android 8.0 Oreo (API level 26) you have to set the importance of the notification's NotificationChannel to IMPORTANCE_MIN:

NotificationChannel channel = 
      new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_MIN);
notificationManager.createNotificationChannel(channel);
...
builder.setChannelId(channel.getId())



回答2:


.setPriority with parameter PRIORITY_MIN will make this possible.

NotificationCompat notification = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.notification_text))
                .setSmallIcon(R.mipmap.ic_launcher)

                //Show the notification only in NotificationDrawer.
                //Notification will not be shown on LockScreen as well as Hidden Icon on StatusBar.
                .setPriority(Notification.PRIORITY_MIN)

                .build();



回答3:


NotificationManager.IMPORTANCE_UNSPECIFIED works in my case




回答4:


You can do this by having a total transparent image and use that as your icon. :)




回答5:


there is no way to show notification without icon.

You can use transparent image. But, it take space of icon.

@CommonsWare: Since the primary point of raising a Notification is to put an icon in the status bar, there is usually no need to not put an icon in the status bar, unless it is interactive, such as a toggle or information notification that will always run and you might want on the pull down, but has no use for an icon.

check this answer for more detail.



来源:https://stackoverflow.com/questions/11373786/how-to-create-a-notification-without-icon-in-the-statusbar-or-simply-hide-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!