2013-04-23 87 views
12
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,但結果是,根本沒有顯示通知!Android Notification.Builder:顯示沒有圖標的通知

+4

只有這樣,我知道的是使用透明圖像的圖標,然後設置通知的順序,以便您的最後(最右邊)使用戶看起來好像沒有圖標。 – FoamyGuy 2013-04-23 13:30:22

+3

這是爲什麼在用戶的興趣? – CommonsWare 2013-04-23 13:34:00

+0

@CommonsWare:一個用例:運行在隱形中的通信應用程序確實希望用戶知道消息何時到達,但不想顯示應用程序圖標或任何圖標以使其他人看不到。 – 2017-08-07 11:58:49

回答

3

你不能這樣做......沒有setSmallIcon(icon,level.level);通知沒有顯示..

17

在果凍豆,以後你可以用nbuilder.setPriority(Notification.PRIORITY_MIN);這個優先級的確切解釋由系統用戶界面決定,但是在AOSP中這會導致通知的圖標被隱藏。

這是針對不需要用戶注意的「環境」或「背景」信息,但如果用戶恰好在通知面板上撥動,她可能會感興趣。有關如何最佳使用通知優先級的更多信息,請參閱Notifications section of the Android Design site

9

更新:不使用它在Android 7,它的崩潰


使用反射,這裏的東西是工作在棒棒堂(仿真器和Moto G的設備)和棉花糖(模擬器)

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

我找不到更好的方法。

這可以在他們更改視圖的ID或更改通知內的任何RemoteViews字段時停止工作,因此使用時風險自負。

0

您可以在不圖標來設置自己的佈局:

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

0

嘗試在通知製造商加入這一行
.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();
所以在這種情況下,你只能在通知欄中看到你的圖標,並且它隱藏在狀態欄中。希望這個幫助。

0

我之前遇到過這個問題,並解決了它這樣的:

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