2016-03-09 39 views
1

我剛剛更新我的android應用程序中的ic_launcher(在Android Studio中製作,順便說一句)。我試圖創建一個測試通知,它曾經工作,但現在它不,我不知道爲什麼。通知不顯示,但不拋出任何錯誤[Android]

Notification.Builder builder = new Notification.Builder(this); 
builder 
     .setWhen(System.currentTimeMillis()) 
     .setContentTitle("TEST") 
     .setContentText("Hello! This is a notification test!") 
     .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

NotificationManager NoMa = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
NoMa.notify(1, builder.build()); 

它甚至不工作,如果我刪除setLargeIcon線,在我的觀點是沒有意義的。我不會放棄我的代碼有問題的可能性。謝謝。

回答

2

如果你想顯示任何通知,你必須setSmallIcon()。 因此將您的代碼更改爲

Notification.Builder builder = new Notification.Builder(this); 
builder 
     .setWhen(System.currentTimeMillis()) 
     .setSmallIcon(R.mipmap.ic_launcher) //Any icon you want 
     .setContentTitle("TEST") 
     .setContentText("Hello! This is a notification test!") 
     .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

NotificationManager NoMa = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
NoMa.notify(1, builder.build()); 
+0

Omg。我不知道setSmallIcon不是可選的,我認爲它應該給我任何錯誤。非常感謝你的傢伙! –