0

Push notification not showing image when the app is closed. Also, it's not redirecting to a particular activity. It's working fine when the app is open or in the background. Here's my code please tell me what I am doing wrong.


    class MyFirebaseMessagingService : FirebaseMessagingService() {
     var bitmap: Bitmap? = null
     override fun onMessageReceived(remoteMessage: RemoteMessage) {
         val type = remoteMessage.data["type"]
         val id = remoteMessage.data["id"]
         val title = remoteMessage.notification!!.title
         val image = remoteMessage.notification!!.icon
         val message = remoteMessage.notification!!.body
         val prf = PrefManager(applicationContext)

        sendNotificationChannel(

            title,
            image,
            message
        )
    }

    private fun sendNotificationCategory(
        id: String?,
        title: String?,
        imageUri: String?,
        iconUrl: String?,
        message: String?
    ) {
        val image = getBitmapfromUrl(imageUri)
        val icon = getBitmapfromUrl(iconUrl)
        val intent = Intent(this, NotificationActivity::class.java)
        intent.action = java.lang.Long.toString(System.currentTimeMillis())
        intent.putExtra("from", "notification")
        val largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
        val NOTIFICATION_ID = id!!.toInt()
        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val CHANNEL_ID = "my_channel_01"
        val name: CharSequence = "my_channel"
        val Description = "This is my channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.description = Description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            mChannel.setShowBadge(false)
            notificationManager.createNotificationChannel(mChannel)
        }
        val builder = NotificationCompat.Builder(
            applicationContext, CHANNEL_ID
        )
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(message)

        if (icon != null) {
            builder.setLargeIcon(icon)
        } else {
            builder.setLargeIcon(largeIcon)
        }
        if (image != null) {
            builder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(image))
        }
        val stackBuilder = TaskStackBuilder.create(
            applicationContext
        )
        stackBuilder.addParentStack(NotificationActivity::class.java)
        stackBuilder.addNextIntent(intent)
        val resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        builder.setContentIntent(resultPendingIntent)
        notificationManager.notify(NOTIFICATION_ID, builder.build())
    }


    private fun sendNotificationUrl(
        id: String?,
        title: String?,
        imageUri: String?,
        iconUrl: String?,
        message: String?,
        url: String?
    ) {
        val image = getBitmapfromUrl(imageUri)
        val icon = getBitmapfromUrl(iconUrl)
        val notificationIntent = Intent(Intent.ACTION_VIEW)
        notificationIntent.data = Uri.parse(url)
        val pi = PendingIntent.getActivity(applicationContext, 0, notificationIntent, 0)
        val largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
        val NOTIFICATION_ID = id!!.toInt()
        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val CHANNEL_ID = "my_channel_01"
        val name: CharSequence = "my_channel"
        val Description = "This is my channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.description = Description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            mChannel.setShowBadge(false)
            notificationManager.createNotificationChannel(mChannel)
        }
        val builder = NotificationCompat.Builder(
            applicationContext, CHANNEL_ID
        )
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(message)
        if (icon != null) {
            builder.setLargeIcon(icon)
        } else {
            builder.setLargeIcon(largeIcon)
        }
        if (image != null) {
            builder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(image))
        }
        val stackBuilder = TaskStackBuilder.create(
            applicationContext
        )
        stackBuilder.addParentStack(NotificationActivity::class.java)
        stackBuilder.addNextIntent(notificationIntent)
        val resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        builder.setContentIntent(resultPendingIntent)
        notificationManager.notify(NOTIFICATION_ID, builder.build())
    }

    private fun sendNotificationChannel(

        title: String?,
        iconUrl: String?,
        message: String?
    ) {
        val image = getBitmapfromUrl(iconUrl)
        val intent = Intent(this, NotificationActivity::class.java)
        intent.action = java.lang.Long.toString(System.currentTimeMillis())
        intent.putExtra("from", "notification")
        intent.putExtra("type", "channel")
        val largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
        val NOTIFICATION_ID = 1
        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val CHANNEL_ID = "my_channel_01"
        val name: CharSequence = "my_channel"
        val Description = "This is my channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.description = Description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            mChannel.setShowBadge(false)
            notificationManager.createNotificationChannel(mChannel)
        }
        val builder = NotificationCompat.Builder(
            applicationContext, CHANNEL_ID
        )
            .setSmallIcon(R.drawable.applogo)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(message)
        if (image != null) {
            builder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(image))
        }
        val stackBuilder = TaskStackBuilder.create(
            applicationContext
        )
        stackBuilder.addParentStack(NotificationActivity::class.java)
        stackBuilder.addNextIntent(intent)
        val resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        builder.setContentIntent(resultPendingIntent)
        notificationManager.notify(NOTIFICATION_ID, builder.build())
    }

    private fun sendNotification(
        id: String?,
        title: String?,
        imageUri: String?,
        iconUrl: String?,
        message: String?
    ) {
        val image = getBitmapfromUrl(imageUri)
        val icon = getBitmapfromUrl(iconUrl)
        val intent = Intent(this, NotificationActivity::class.java)
        intent.action = java.lang.Long.toString(System.currentTimeMillis())
        intent.putExtra("id", id!!.toInt())
        intent.putExtra("from", "notification")
        intent.putExtra("type", "poster")
        val largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
        val NOTIFICATION_ID = id.toInt()
        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val CHANNEL_ID = "my_channel_01"
        val name: CharSequence = "my_channel"
        val Description = "This is my channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            mChannel.description = Description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            mChannel.setShowBadge(false)
            notificationManager.createNotificationChannel(mChannel)
        }
        val builder = NotificationCompat.Builder(
            applicationContext, CHANNEL_ID
        )
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(message)
        if (icon != null) {
            builder.setLargeIcon(icon)
        } else {
            builder.setLargeIcon(largeIcon)
        }
        if (image != null) {
            builder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(image))
        }
        val stackBuilder = TaskStackBuilder.create(
            applicationContext
        )
        stackBuilder.addParentStack(NotificationActivity::class.java)
        stackBuilder.addNextIntent(intent)
        val resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        builder.setContentIntent(resultPendingIntent)
        notificationManager.notify(NOTIFICATION_ID, builder.build())
    }

    /*
     *To get a Bitmap image from the URL received
     * */
    fun getBitmapfromUrl(imageUrl: String?): Bitmap? {
        return try {
            val url = URL(imageUrl)
            val connection =
                url.openConnection() as HttpURLConnection
            connection.doInput = true
            connection.connect()
            val input = connection.inputStream
            BitmapFactory.decodeStream(input)
        } catch (e: Exception) {
            // TODO Auto-generated catch block
            e.printStackTrace()
            null
        }
    }

    companion object {
        private const val TAG = "FCM Service"
    }
  }

This code is working while the app is in the background but I want a push notification with an image while the app is closed. I'm using retrofit for API calling and I'm getting notifications from the server.

  • 1
    If the app is closed then the Notification appears in Notification Tray Check my answer here https://stackoverflow.com/a/54787937/5335382 – Shivam Yadav Aug 03 '22 at 06:33

1 Answers1

0

Regarding the images: did you check if the urls are null/empty? I see that they can be null, and you checked it with a try/catch, but maybe this could be the problem.

I also had problems with notifications showing/not showing up when in different states of the app. What helped me was to override the handleIntent method in my MessagingService class. For example:

override fun handleIntent(intent: Intent?) {
        try {
            if (intent?.extras != null) {
                val builder = RemoteMessage.Builder("where_to_string")
                for (key in intent.extras!!.keySet()) {
                    builder.addData(key!!, intent.extras?.getString(key))
                }
                onMessageReceived(builder.build())
            } else {
                super.handleIntent(intent)
            }
        } catch (e: Exception) {
            super.handleIntent(intent)
        }
    }

You can play with this method to customise to your needs (check if the app is in the background or not, filter the extras etc).

E. P.B.
  • 1
  • 1