Estoy creando una notificación, deseo que al hacer click en ella se ejecute una función.
//CREO EL CANAL
fun createNotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val importance = NotificationManager.IMPORTANCE_HIGH
val channel: NotificationChannel = NotificationChannel(channelId,channelName, importance)
val manager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
}
}
Uso firebaseMessaging para enviar la notificación, luego para mostrarla cuando la app esta en primer plano uso lo siguiente.
class MyFirebaseMessagingService: FirebaseMessagingService() {
override fun onMessageReceived(p0: RemoteMessage) {
App.getMainActivity()?.showNotify(p0.notification?.title.toString(),
p0.notification?.body.toString()
//Extraigo el titulo y contenido luego los envió como parámetro para que se cree la notificación.
)
}
}
fun showNotify(tlNtf:String,ctnNtf:String){
val notification = NotificationCompat.Builder(this,channelId).also {
it.setContentTitle(tlNtf)
it.setContentText(ctnNtf)
it.setSmallIcon(R.drawable.ic_app_fg)
it.color = Color.RED
it.priority = NotificationCompat.PRIORITY_HIGH
}.build()
val notificationManager: NotificationManagerCompat = NotificationManagerCompat.from(this)
notificationManager.notify(notificationId,notification)
}
Necesito que al hacer click en la notificación se ejecute una función, he leído la documentación sin embargo no encontré nada acerca de eso, se que se puede abrir una actividad con un intent, sin embargo solo necesito que ejecute una función de la actividad en la que me encuentro.
Además no tiendo por que la notificación no se muestra arriba de la pantalla es decir, llega correctamente pero se va directamente al panel de notificaciones.
Ejemplos de como debería aparecer según un codelab, no aparece de esta forma solo aparece directamente en el panel de notificacion, hay algo que este asiendo mal?