简体   繁体   中英

Notification icon is not changed with .setSmallIcon

I am implementing Notifications in android . I want to change the icon of notification, but the problem is that setSmallIcon is not working. My custom icon is showing in the lollipop device but not showing the custom icon above lollipop devices.

I have tried the following code:

send_notification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.book2);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("New Book Added")
                        .setContentText("Android with java")
                        .setStyle(new NotificationCompat.BigPictureStyle()
                                    .bigPicture(icon)
                                    .bigLargeIcon(null));
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    builder.setSmallIcon(R.drawable.ic_notification);
                } else {
                    builder.setSmallIcon(R.drawable.notification);
                }
                NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.notify(1,builder.build());
            }
}

The big picture is also not showing. Please Help.

EDIT:

Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.test);

    String channelId = getString(R.string.default_notification_channel_id);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);

    if (remoteMessage.getData().size() > 0) {
        String title = remoteMessage.getData().get("title");
        String text = remoteMessage.getData().get("body");

        builder.setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(text)
                .setLargeIcon(icon)
                .setPriority(Notification.PRIORITY_MAX)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigLargeIcon(icon));

    } else if (remoteMessage.getNotification() != null) {
        String title = remoteMessage.getNotification().getTitle();
        String text = remoteMessage.getNotification().getBody();

        builder.setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(text)
                .setLargeIcon(icon)
                .setPriority(Notification.PRIORITY_MAX)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigLargeIcon(icon));

    }

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel notificationChannel = new NotificationChannel(channelId, "Testing channel", NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(notificationChannel);

    }
    manager.notify(1, builder.build());

Try uninstalling and reinstalling the 100% fresh App. Android keeps cache so things work more quickly and the notification is one of those things.

Uninstalling and reinstalling worked for my issue some time ago.

Also do remember that in Android Oreo and above, in order for notifications to work; notification channels must be created and also the notification that you're creating should be assigned to one of them.

EDIT

What I've tried (and it works for me): (Sample in Kotlin but should be similar enough);

fun onClick(view: View) {
    var icon_resource = R.drawable.ic_mtrl_chip_close_circle
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
        icon_resource = R.drawable.ic_mtrl_chip_checked_circle
    }

    val icon = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher_round)

    val notification =  NotificationCompat.Builder(this)
        .setSmallIcon(icon_resource)
        .setContentTitle("New Book Added")
        .setContentText("Android with java")
        .setLargeIcon(icon)
        .setStyle(NotificationCompat.BigPictureStyle()
            .bigPicture(icon)
            .bigLargeIcon(null)).build()

   val manager=  getSystemService(NOTIFICATION_SERVICE) as NotificationManager
   manager.notify(1,notification)
}

SECOND EDIT

I think there's a problem with your image, (as there was in mine; sizes or something). After spending a lot of time trying to figure out why it didn't display the launcher icon in the notifications I decided to try with one of my images that I had laying around...

Tested with Android O; API 28

And here are the results:

折叠 展开

Feel free to try with the original image I used: WTF_Gopher

Here's the code that is powering these results; it's not very different from the first one...

// Parametherise this drawable with an if statement of your own
var icon_resource = R.drawable.ic_mtrl_chip_close_circle

val manager=  NotificationManagerCompat.from(this)
// This is required because the testing device is an Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    manager.createNotificationChannel(
        NotificationChannel("CUSTOM_NOTIFICATIONS", "StackOverflow", NotificationManager.IMPORTANCE_HIGH)
    )
}

val icon = BitmapFactory.decodeResource(resources,R.mipmap.dafuq)

val notification =  NotificationCompat.Builder(this, "CUSTOM_NOTIFICATIONS")
    .setSmallIcon(icon_resource)
    .setContentTitle("New Book Added")
    .setContentText("Android with java")
    .setLargeIcon(icon)
    .setStyle(NotificationCompat.BigPictureStyle()
        .bigPicture(icon)
        .bigLargeIcon(null)).build()

manager.notify(1,notification)

Additional info:

When I'm using a wrong image, I get this pesky log in my Logcat. Check for the same or similars! 2019-04-10 20:11:02.120 3434-3434/com.example.deletemeapp D/skia: --- Failed to create image decoder with message 'unimplemented'

What you want to do is create the notification icon as follows:

        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                .setContentTitle("New Book Added")
                .setContentText("Android with java");

Now Add:

        NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
        s.setSummaryText("Summary text appears on expanding the notification");
        //Set the style of the notification to this big picture style
        builder.setStyle(s);

For the versioning add this:

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
       // Marshmallow+
   }else{
        //below Marshmallow
}

Hope this helps :)

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