2020-07-12-Anroid-EventBus

build.gradle (Module: app)

dependencies {
    ...
    implementation 'org.greenrobot:eventbus:3.2.0'
    ...
}

NotificationData.kt

Data which needs to be sent. You can create your own data class.

data class NotificationData(
    val body: String,
    val clickAction: String,
    val title: String
)

MyFirebaseMessagingService.kt - Sender

MyFirebaseMessagingService: FirebaseMessagingService() {

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)
        val messageText = message.notification?.body ?: message.data.get("body") ?: "No Message Body"
        var clickAction = message.notification?.clickAction ?: ""
        val title = message.notification?.title ?: "Notification"
        EventBus.getDefault().post(NotificationData(messageText, clickAction, title))
    }

}

MainActivity.kt - Receiver

Last updated

Was this helpful?