Sagar Balyan
Sagar Balyan

Reputation: 658

setSmallIcon() and setLargeIcon() not working in Android Notification

I am pushing the notification from the Firebase Console. I am able to easily receive the notification data and notification also appears but what i am trying to do is change the small icon and the large icon of the notification.

I am using both the methods but none of them seem to work. I have also tried making the small icon using the Vector option via res>New>Vector>Clip Art. Neither small icon appears nor does the large icon and the notification is also not expandable.

MessagingService.kt

class MessagingService(): FirebaseMessagingService() {

  override fun onMessageReceived(p0: RemoteMessage ? ) {
    super.onMessageReceived(p0)
    showNotification(p0!!.notification!!.title!!, p0!!.notification!!.body!!)
  }

  fun showNotification(title: String, body: String) {
    val icon = BitmapFactory.decodeResource(resources,
      R.drawable.iphn)
    NotificationCompat.Builder(this, "MyNotifications")
      .setLargeIcon(icon)
      .setSmallIcon(R.drawable.ic_notif)
      .setContentTitle(title)
      .setContentText(body)
      .setStyle(NotificationCompat.BigPictureStyle()
        .bigPicture(icon)
        .bigLargeIcon(null))
      .build()

  }
}

ic_notif is the drawable i created using Vector

Upvotes: 1

Views: 5784

Answers (3)

user9923632
user9923632

Reputation:

Here is working example :

call sendNotification(yourResponse) inside onMessageReceived(remoteMessage: RemoteMessage?) this way you can set large icon statically or dynamically.

private fun sendNotification(response: NotifyResponse) {
        var bmp: Bitmap? = null
        try {
//loading the image from server 


//            if (!TextUtils.isEmpty(response.image)) {
//                val futureTarget = GlideApp.with(this).asBitmap().load(response.image).submit()
//                try {
//                    bmp = futureTarget.get()
//                    GlideApp.with(this).clear(futureTarget)
//
//                } catch (e: ExecutionException) {
//                    e.printStackTrace()
//                } catch (e: InterruptedException) {
//                    e.printStackTrace()
//                }
//
//            }

            val uniqueInt = (System.currentTimeMillis() and 0xfffffff).toInt()
            val pendingIntent = PendingIntent.getActivity(this, uniqueInt, getIntent(response.type), PendingIntent.FLAG_ONE_SHOT)

            val channelId = BuildConfig.FLAVOR
            val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
            val notificationBuilder = NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(notificationIcon)
                    .setContentTitle(response.title)
                    .setContentText(response.message)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
            if (bmp != null) {
                notificationBuilder.setLargeIcon(bmp)
                notificationBuilder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bmp).bigLargeIcon(null))
            }
            notificationBuilder.setContentIntent(pendingIntent)


            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(BuildConfig.FLAVOR,
                        "Channel human readable title",
                        NotificationManager.IMPORTANCE_DEFAULT)
                notificationManager.createNotificationChannel(channel)
            }

            notificationManager.notify(0, notificationBuilder.build())
        } catch (o: Exception) {
            o.printStackTrace()
        }

    }

Upvotes: 0

Dhara Vamja
Dhara Vamja

Reputation: 604

You can try using ic_notif.png instead of a vector.

Other than that, In the latest Android version, It is recommended to use channelId. you can add this block to add channelId

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channelId = "yourChannelId"
        val channel = NotificationChannel(channelId, "your channel Name" ,
                NotificationManager.IMPORTANCE_DEFAULT)
        mNotificationManager.createNotificationChannel(channel)
        mBuilder.setChannelId(channelId)
    }

Upvotes: 1

nisha.113a5
nisha.113a5

Reputation: 2024

private var mBuilder: NotificationCompat.Builder? = null  
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val intent = Intent(activity, MainActivity::class.java)
val pi = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)


  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {            
     val icon = BitmapFactory.decodeResource(resources, shapemyapp.admin.R.drawable.iphn)                                    
     val importance = NotificationManager.IMPORTANCE_DEFAULT
     val notificationChannel = NotificationChannel("ID", "Name", importance)
     mNotificationManager.createNotificationChannel(notificationChannel)
     mBuilder = NotificationCompat.Builder(this, "MyNotifications")
          .setLargeIcon(icon)
          .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
          .setContentTitle(title)
          .setContentText(body)              
          .build()
  } else {
        mBuilder = NotificationCompat.Builder(activity) 
           .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
  }                                         
    mBuilder!!.setContentIntent(pi)
    mNotificationManager.notify(NotificationID.id, mBuilder!!.build())

Upvotes: 0

Related Questions