简体   繁体   中英

Android : Notification not sent & shown when Icon is changed

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

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

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. 通知图标生成Android Studio

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. Android Studio中的透明图标

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM