Search code examples
androidnotificationsstylespush

How to create push notification with long text and picture in one style


How to create push notification with long text and picture in one notification style?

I'm using BigTextStyle for showing push notification, because I need to show big text, but how can I add an image ?

Or I need to use some custom notifications?

some example:

enter image description here


Solution

  • I hope this help you. Let me know if you found difficulties in this

    private fun showBigNotification(
        bitmap: Bitmap,
        mBuilder: NotificationCompat.Builder,
        icon: Int,
        title: String,
        message: String,
        resultPendingIntent: PendingIntent,
        alarmSound: Uri
    ) {
    
    
        val CHANNEL_ID = "123456789"
        val name: CharSequence = mContext.getString(R.string.app_name)
        val importance = NotificationManager.IMPORTANCE_HIGH
        val inboxStyle = NotificationCompat.InboxStyle()
        val notificationManager =
            mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val audioAttributes = AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") val mChannel =
                NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.setSound(alarmSound, audioAttributes)
            mChannel.enableLights(true)
            mChannel.enableVibration(true)
            notificationManager.createNotificationChannel(mChannel)
        }
    
    
        val bigPictureStyle = NotificationCompat.BigPictureStyle()
        bigPictureStyle.setBigContentTitle(title)
        bigPictureStyle.setSummaryText(Html.fromHtml(message).toString())
        bigPictureStyle.bigPicture(getResizedBitmap(bitmap, bitmap.width))
        val notification: Notification
        val notificationBuilder = NotificationCompat.Builder(
            mContext, CHANNEL_ID
        )/*
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
            */
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setSound(alarmSound)
            .setStyle(bigPictureStyle)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.resources, icon))
            .setContentText(message)
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setSmallIcon(R.drawable.ic_stat_name)
            notificationBuilder.color = mContext.resources.getColor(R.color.colorPrimary)
        } else {
            notificationBuilder.setSmallIcon(icon)
        }
        val id = Random(System.currentTimeMillis()).nextInt(1000)
        notificationManager.notify(id, notificationBuilder.build())
    
    }