![](/img/trans.png)
[英]Firebase notification icon not shown on Android 5.0 and newer
[英]Android : Notification not sent & shown when Icon is changed
我正在開發一個Android項目,無論何時發生事件,我都會在其中發送通知。 不幸的是,當我將“通知”圖標更改為我們的項目圖標(這是一個8.4kb的圖像)時,沒有收到任何通知。 這是特別有問題的,因為不會引發任何錯誤,只會收到任何通知。
當我將圖像更改為簡單的紅色正方形時,可以看到通知,但是通知甚至不是紅色。 如何將通知圖像正確設置為所需圖像。 謝謝。
如您所見,第一個通知是圖標不正確。
屏幕截圖:
代碼:
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++;
}
我嘗試設置的圖像是8kb的PNG圖像,無濟於事。 你能幫忙的話,我會很高興。 謝謝。
更新資料
即使IDE正確顯示它,通知中收到的圖像也不正確。
現在,當我嘗試將其添加為資產時,它顯示預覽非常錯誤。 並且生成的圖像也是錯誤的。
屏幕截圖:
如您所見,它只是說圖像是灰色,但是是藍色圖像。
阿羅克斯的建議
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));
您需要實現新的通知規范,需要使用setLargeIcon()以及透明的小圖標。
在您的情況下,您僅使用setSmallIcon()會導致問題。 只需設置大小圖標即可使用(這是棒棒糖的更新)
更新:如果只需要設置小圖標,則不需要使用位圖並應用一些背景色
嘗試從Android Studio中將其作為可繪制的圖像獲取為通知圖標。 也可以使用setLarge()圖標。 選擇資源時,選擇它是圖像資產,如下所示。
通知圖標會預覽顯示基於API級別的外觀。
對於您的圖標,您需要將其用作透明圖標,然后以上述方法本身導入。 從圖標中刪除藍色層,然后將其加載到Android Studio中。 您將如下所示。
然后,如果您仍想在圖標中使用藍色,則需要以編程方式將背景色添加到NotificationCompat.Builder中,如下所示:
int notificationcolor = getResources().getColor(R.color.my_notif_color);
mBuilder.setColor(notificationcolor);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.