0

I have an issue in callback interface initialization.

I have to pass the push notification data to my MainActivity (HomeActivity).

So, I have a listener (interface object) as below :

    class FirebaseMessageReceiver : FirebaseMessagingService() {
...
    lateinit var pushNotificationArrivedListener:PushNotificationListener
...

}

Inside the method :

 override fun onMessageReceived(remoteMessage: RemoteMessage) {

if (this::pushNotificationArrivedListener.isInitialized && pushNotificationArrivedListener != null) {
            pushNotificationArrivedListener.onPushNotificationArrived(firmwareObj)
        }

}

Where, In same class the interface is created as below :

interface PushNotificationListener {
    fun onPushNotificationArrived(firmwareObj:FirmwarePushNotification)
}

Overriding it in HomeActivity as below :

override fun onPushNotificationArrived(firmwareObj: FirmwarePushNotification) {
        Log.e("&&&&& ","&&&&& onPushNotificationArrived")
        
    }

Implementing and Initiating it as below in HomeActivity :

class HomeActivity : AppActivity(),FirebaseMessageReceiver.PushNotificationListener {

and inside onCreate() initializing the interface as below :

firebaseMessageReceiver.initializePushNotificationListener(this)

WHERE,

firebaseMessageReceiver is initialized in App.kt class as below :

        var firebaseMessageReceiver: FirebaseMessageReceiver = FirebaseMessageReceiver()

Everything is looks correct, But listener is not getting initialized.. i.e. below if condition is not satisfying :

 if (this::pushNotificationArrivedListener.isInitialized && pushNotificationArrivedListener != null) {

What might be the issue?

EDIT Below is the method also inside class :

fun initializePushNotificationListener(pushNotificationArrivedListener: PushNotificationListener) {
    this.pushNotificationArrivedListener = pushNotificationArrivedListener
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • Is your `onMessageReceived` even triggered? – Alex Mamo Sep 13 '21 at 11:36
  • Yes, Its triggred and I am also getting exception if not checking that Its null or not. – Jaimin Modi Sep 13 '21 at 11:45
  • 2
    We don't need to initialize FirebaseMessageReceiver class as it will be initialized by the app that's why it gives null every time with your listener. – Mohit Dholakia Sep 13 '21 at 12:49
  • @MohitDholakia thanks. Do you have any idea that How can we initialize listener inside FirebaseMessageReceiver class ? means how to call method initializePushNotificationListener() in activity.? – Jaimin Modi Sep 13 '21 at 12:51
  • @Jaimin As far as I know, we can't get that object of FirebaseMessageReceiver class so to pass data from FCM class to activity you should use RXBus or LocalBroadCastManager. – Mohit Dholakia Sep 13 '21 at 12:58
  • 2
    You can't implement listeners like that from `FirebaseMessagingService()` since that service is not initialised by your app. Few ways to achieve that is by either using local broadcast managers, or by implement pending intent to receive intent data into activity or else storing data into database and then receiving it on activity by db. – Jeel Vankhede Sep 13 '21 at 13:58

0 Answers0