android

Android Notification bigLargeIcon: Overload resolution ambiguity. All these functions match


I am tryting to display a large image in the notification. I am following the docs. But, the IDE is showing the error:

enter image description here

This is my code:

private fun getNotificationBuilder(): NotificationCompat.Builder {
    // this intent opens the MainActivity when the user taps on the notification
    val intentNotification = Intent(context, MainActivity::class.java)
    val pendingIntentNotification = PendingIntent.getActivity(
        context,
        exampleNotificationID,
        intentNotification,
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )

    val myBitmap = BitmapFactory.decodeResource(
        context.resources,
        R.drawable.puppy
    )

    val builder = NotificationCompat.Builder(context.applicationContext, exampleChannelID)
        .setSmallIcon(R.drawable.chair)
        .setContentIntent(pendingIntentNotification)
        .setLargeIcon(myBitmap)
        .setStyle(NotificationCompat.BigPictureStyle()
            .bigPicture(myBitmap)
            .bigLargeIcon(null)
        )

    return builder
}

These are the improts:

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.core.app.NotificationCompat

Anyone knows how to fix this?


Solution

  • Ideally the BigPictureStyle API would allow you to say "no large icon, #kthxbye" without passing null to a function, let alone one with multiple null-capable overloads.

    Peeking at the source suggests that it doesn't matter which of the two overloads you wind up calling, so add a cast to one of them:

        val builder = NotificationCompat.Builder(context.applicationContext, exampleChannelID)
            .setSmallIcon(R.drawable.chair)
            .setContentIntent(pendingIntentNotification)
            .setLargeIcon(myBitmap)
            .setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(myBitmap)
                .bigLargeIcon(null as Bitmap?)
            )
    

    (you could use Icon?, but that requires a minimum API of 23)