3

我为我的 Kotlin 应用程序构建了一个 FCM 服务,它是一个活动和多个片段类型的应用程序。我正在使用导航图从一个片段到下一个片段。我能够看到推送通知进入数据和通知。每个通知都有一个自定义值调用 notify_type。我可以在服务中很好地查看和读取此变量。根据 notify_type 值,我想打开不同的片段。我想使用导航图控制器打开。有人可以帮我弄清楚从 FCM 服务打开片段需要做什么吗?蒂亚!

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")
        }
    }
}
4

0 回答 0