Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate to a fragment with navArgs using notification

I'm new with working with notifications in my app so please bear with me. I have a pending intent which will navigate to MainActivity when the user clicks on it. I want to change it so that it navigates to a fragment that has navigation arguments.

My code for pending intent

        val notifyIntent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            data = Uri.parse(uri)
        }
        return PendingIntent.getActivity(
            context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
        )

Navigation action for my fragment

       NavigationGraphMainDirections.actionGlobalManagePostFragment("data")

I don't know if I have to send the string of data with the notification and get it back with the pending intent. What is the solution here?

like image 962
Maryam Mirzaie Avatar asked Dec 28 '19 11:12

Maryam Mirzaie


1 Answers

The best way to achieve what you want is to use an explicit deep link.

Create your pending intent as in:

val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.android)
.setArguments(args)
.createPendingIntent()
like image 194
BMacedo Avatar answered Sep 19 '22 10:09

BMacedo