2016-04-04 42 views
0

我正在開發一個Android項目,在該項目中,當發生事件時,我會發送通知。 不幸的是,當我將通知圖標更改爲我們的項目圖標(8.4kb圖像)時,我沒有收到任何通知。這是特別有問題的,因爲沒有錯誤發生,只是沒有收到通知。Android:當圖標發生變化時,通知未發送並顯示

當我將圖像更改爲簡單的紅色方塊時,我可以看到通知,但通知甚至沒有變成紅色。如何正確設置通知圖像爲所需的圖像。謝謝。

正如你所看到的第一個通知,該圖標是不正確的。

截圖:

enter image description here

代碼:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()); 
mBuilder.setAutoCancel(true); 

mBuilder.setSmallIcon(R.drawable.defaultimage); 
mBuilder.setContentTitle(subject); 
mBuilder.setContentText(Html.fromHtml(text)); 

if (type.equals("note")) { 
        Log.d("type","note"); 
        Intent resultIntent = new Intent(getApplication(), EditNoteActivity.class); 
        resultIntent.putExtra("groupid", Long.valueOf(channelName)); 
        resultIntent.putExtra("canvasid", Integer.valueOf(canvasId)); 
        resultIntent.putExtra("sectionid", Integer.valueOf(sectionId)); 
        resultIntent.putExtra("noteid", Integer.valueOf(noteId)); 

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext()); 
        stackBuilder.addParentStack(EditNoteActivity.class); 

        stackBuilder.addNextIntent(resultIntent); 
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
        mBuilder.setContentIntent(resultPendingIntent); 

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

        mNotificationManager.notify(notificationCounter, mBuilder.build()); 
        notificationCounter++; 
} 

,我想設置的圖像是一個PNG圖像,8KB的,不利於它設置任何方式。你能幫忙的話,我會很高興。謝謝。

更新

當我選擇圖像時,IDE正常顯示圖像從截圖看出: enter image description here

即使IDE正確地顯示它,接收的圖像中的通知是不正確。

現在,當我嘗試將其添加爲資產時,它顯示預覽非常錯誤。而生成的圖像也是錯誤的。

截圖:

enter image description here

正如你所看到的,它只是說,圖像是一些灰色的顏色,但它有一個藍色的圖像。

Aloks建議

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()); 
       mBuilder.setAutoCancel(true); 
       mBuilder.setSmallIcon(R.mipmap.twentynotelogo); 
       Bitmap icon = BitmapFactory.decodeResource(getResources(), 
         R.mipmap.twentynotelogo); 
       mBuilder.setLargeIcon(icon); 
       mBuilder.setContentTitle(subject); 
       mBuilder.setContentText(Html.fromHtml(text)); 
+0

,您直接使用PNG圖像從Android應用程序與你的繪製文件夾在通知生成器方法調用中設置? –

+0

@ShadabK:是的。我嘗試過,並嘗試將它作爲位圖使用該位圖,但都沒有奏效。它只是顯示您現在在屏幕截圖中看到的框。 –

回答

0

您需要執行通知新的規範, 需要與透明小圖標使用setLargeIcon()在一起。

在你的情況下,你正在使用setSmallIcon()只會導致問題。 只需設置小和大圖標,將工作(這是在棒棒糖更新)

更新:如果你只需要設置小圖標比你需要使用位圖和應用一些背景顏色

+0

你能否詳細說明我如何用小圖標和我想要的圖像來做到這一點? –

+0

如果您的小圖標需要做任何特別的事情,請注意圖片應該是單色,並且具有透明度,否則完整的圖片會受到系統自動應用的顏色過濾的影響,在通知中顯示小圖標 – Alok

+0

好的,我會嘗試現在這個。 Moment –

0

嘗試將圖像作爲Android Studio的可繪製圖像作爲通知圖標。 還使用setLarge()圖標。 選擇資源時選擇它是一個圖像資產,如下所示。 Notification icon generation Android Studio

通知圖標在預覽中顯示基於API級別的外觀。

對於您的圖標,您需要將其用作透明圖標,然後導入上述方法本身。從圖標中移除藍色圖層,然後在Android Studio中加載它。你會得到如下所示。 Transparent Icon in Android Studio

然後,如果你仍然想在你的圖標,藍色,你需要以編程方式添加背景色您NotificationCompat.Builder作爲

int notificationcolor = getResources().getColor(R.color.my_notif_color); 

mBuilder.setColor(notificationcolor); 
+0

預覽顯示一個灰色的圖像,而不是實際的預覽。這是正常的嗎? –

+0

是的,它是Android Studio如何生成圖標,您需要爲其提供適當的圖像,以便它正確地反轉。 –

+0

好的,資產工作室創建了4個圖像並將它們放置在mimmap - ****中...您能告訴我如何訪問圖像嗎? –

相關問題