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
class MainActivity : AppCompatActivity() {
private var isAppInForeground = false
override fun onCreate() {
super.onCreate()
EventBus.getDefault().register(this)
}
override fun onDestroy() {
super.onDestroy()
EventBus.getDefault().unregister(this)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onThrowEvent(data : NotificationData) {
if (data.clickAction.isNotEmpty()) {
val snack = Snackbar.make(
this@MainActivity.containerView,
data.body,
Snackbar.LENGTH_LONG
)
snack.setAction("Dismiss", View.OnClickListener {
println("User pressed Dismiss button on snackbar")
})
snack.show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}