5

I've built a FCM service for my Kotlin app, which is one activity and multiple fragment type app. I'm using the navigation graph to get from one fragment to the next. I'm able to see the push notifications come in for data and notification. Each notification has a custom value call notify_type. I can see and read this variable fine in the service. Based on the notify_type value, I want to open different fragments. And I'd like to open using the navigation graph controller. Can someone help me figure out what needs to be done to open fragments from the FCM service? TIA!

override fun onMessageReceived(remoteMessage: RemoteMessage?) {

    remoteMessage?.let {
        it.data?.let { itDT ->
            Log.d(TAG, "Message data payload: $itDT")

            val notifyType = itDT["notify_type"]

            when (notifyType) {

                "public_profile" -> {

                    val userId = itDT["user_id"]

                    //need to open public profile fragment sending user id
                    //would normally use this. Obviously doesn't work in a service
                    val navController = it.findNavController()
                    val action = HomeFragmentDirections.actionNavigationHomeToPublicProfileFragment(
                        userId
                    )
                    navController.navigate(action)

                } else -> {

                    Log.d(TAG, "Unsupported notifyType: $notifyType")

                }
            }
        }

        it.notification?.let { itBD ->
            Log.d(TAG, "Message Notification Body: $itBD")
        }
    }
}
vazy
  • 73
  • 1
  • 4

0 Answers0