0

I can receive the notification sent from Firebase Cloud Messaging and show it to android device in here

enter image description here

when I click notification from my app, I expect it will go inside my app. but when I click the notification from my app it just doesn't do anything. it seems it can't be clicked.

so how to make it go to inside my app when I click the notification. here is the code I use

creating channel using this code

class App : Application() {

    override fun onCreate() {
        super.onCreate()

        createNotificationChannels()
    }

    private fun createNotificationChannels() {

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

            val infoChannel = NotificationChannel(
                NOTIFICATION_CHANNEL_INFO,
                NOTIFICATION_CHANNEL_INFO,
                NotificationManager.IMPORTANCE_DEFAULT
            )

            val importantNews = NotificationChannel(
                NOTIFICATION_EVENT_UPDATES,
                NOTIFICATION_EVENT_UPDATES,
                NotificationManager.IMPORTANCE_HIGH
            )

            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(infoChannel)
            notificationManager.createNotificationChannel(importantNews)

        }
    }


}

and build notification using this code

private fun setUpNotification(title: String, message: String, type: String, imagePath: String) {

        val channel = if (type == "Event Updates") NOTIFICATION_EVENT_UPDATES else NOTIFICATION_CHANNEL_INFO

        var notification = NotificationCompat.Builder(this, channel)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)


        if (type == "Event Updates") {
            notification = notification
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_EVENT)
        } else {
            notification = notification
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
        }


        if (imagePath.isNotEmpty()) {

            Glide.with(this)
                .asBitmap()
                .load(imagePath)
                .into(object: SimpleTarget<Bitmap>() {

                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        notification = notification.setLargeIcon(resource)
                        val build = notification.build()
                        notificationManager.notify(1, build)
                    }

                })

        } else {

            val build = notification.build()
            notificationManager.notify(1, build)

        }


    }



}
Alexa289
  • 8,089
  • 10
  • 74
  • 178

1 Answers1

0

you have to use pending intent to achive this

   val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, Intent(context, TimerService::class.java), 0)

    notification.setContentIntent(pendingIntent)
gowtham6672
  • 999
  • 1
  • 10
  • 22