Reputation: 5313
I am working on an Android project in which I am sending out notifications whenever there is an event happening. Unfortunately, when I change the icon of Notification to our project icon, which is a 8.4kb image, I don't get any notification. This is especially problematic as there is no error thrown, just no notificiations are received.
When I change the image to a simple red-square, I can see the notification, but the notification is not even red colored. How can I properly set the Notification image to desired image. Thank you.
As you can see the first notification, the icon is not proper.
Screenshot :
Code :
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++;
}
The image which I am trying to set is a PNG image, of 8kb, doesn't help setting it any way. Any help would be nice. Thank you.
Update
When I select the image, The ide shows the image properly as seen from the screenshot :
Even if the IDE shows it correctly, the image received in notification is not correct.
Now, when I try to add it as an asset, it shows preview very wrong. And the images generated are also wrong.
Screenshot :
As you can see, it just says the image is some gray color, but its a blue colored image.
Aloks suggestion
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));
Upvotes: 0
Views: 731
Reputation: 1755
Try to get the image as a drawable from the Android Studio as a Notifications Icon.
Also use setLarge() Icon as well.
While selecting the resource select it is a Image asset as shown below.
The notification icon shows in preview what it will look like based on the API levels.
For your icon you need to use it as a Transparent Icon and then import in the above method itself. Remove the blue layer from the icon then load it in the Android Studio. You will get this as shown below.
Then if you still want blue color in your icon you need to add background color programmatically to your NotificationCompat.Builder as
int notificationcolor = getResources().getColor(R.color.my_notif_color);
mBuilder.setColor(notificationcolor);
Upvotes: 0
Reputation: 881
You need to implement new specification for notification, Need to use setLargeIcon(),together with transparent small icon.
in your case you are using setSmallIcon() only that is causing issue. Just set both small and large icon it will work.( it is an update in lollipop)
update: if you only need to set small icon than you need to use bitmap and apply some background color
Upvotes: 0