0

I'm attempting to trigger a specific tab to open with some data on notification press. The way the notification is setup is it open the root page if the user clicks the notification itself and a specific page if they click a button on the notification.

The application is using the Navigation component for navigation. To allow for the navigation from the notification to the page I added a deeplink using the pattern I have used across the app.

The page I'm trying to navigate to is a fragment that uses a FragmentPagerAdapter to display tabs.

So in the navigation graph I added:

 <fragment
    android:id="@+id/tabFragment"
    android:name="TabFragment"
    android:label="@string/label_for_tab_fragment"
    tools:layout="@layout/fragment_tabs">

    <deepLink app:uri="app://test?page={page}" />

    <argument
        android:name="page"
        app:argType="integer" />
</fragment>

Then in the notification I built

val uri = "app://test"

val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.data = Uri.parse(uri)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)

val (shortDescription, longDescription) = generateContentString(info)
val style = NotificationCompat.BigTextStyle().bigText(longDescription)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.logo)
        .setContentTitle(title)
        .setContentText(shortDescription)
        .setStyle(style)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setOnlyAlertOnce(true)
        .setOngoing(true)

val pageIntent = Intent()
pageIntent.action = Intent.ACTION_VIEW
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val intentUri = Uri.parse(uri).buildUpon()
        .appendQueryParameter("page", 1.toString())
        .build()
pageIntent.data = intentUri
val pagePendingIntent = PendingIntent.getActivity(this, 1, pageIntent, PendingIntent.FLAG_ONE_SHOT)
builder.addAction(R.drawable.ic_edit, buttonTitle, pagePendingIntent)

The notification body selection correctly launches the page, but the button does not. I figure this could be an issue with the deeplink if passing params so I tested this with adb. When I did this it correctly launched the page and included the variable with the value of 1.

adb shell am start -W -a android.intent.action.VIEW -d "app://test?page=1"

I'm not really sure what could be going on any insight would be useful. I also tried this with link in an email and seemed to work correctly.

Also using the findNavController method to create a pending intent works. However I'm using a long running service as such I can't use the findNavController method

val args = bundleOf("page" to 1)
val deeplink = findNavController().createDeepLink()
    .setDestination(R.id.tabFragment)
    .setArguments(args)
    .createPendingIntent()

A bit more background the notification is a being launch by a service using startForeground.

I isolated the pageIntent a bit more, by invoking it from a fragment button click that grabs the parent activity and launch the intent with startActivity. This correctly start the intent and passed the page value. Which means it has to do with the pending intent wrapper.

I thought this could be cause by me not making the pending intent id unique, but this did not solve the problem. Update the code to reflect the issue.

CubanAzcuy
  • 125
  • 1
  • 13
  • Your `` uses `app:test/tabs` as the path, but that isn't what your `uri` is in your code. Why don't those match? – ianhanniballake Jan 29 '21 at 19:07
  • It was a typo in my post (not in the original code), I updated the code snippet. This does not change the result it fix the issue. – CubanAzcuy Jan 29 '21 at 23:06
  • Does everything work if you change everything to use `app://test/test` rather than a Uri that has no path? – ianhanniballake Jan 30 '21 at 00:29
  • The path doesn't make a difference it, it will resolve when using a url or adb, but fail from the pending intent. It seems to have something to do to with the pending intent specifically when using params. I have tried to use the uri query path builder and directly add the variables by using kotlin string template function `"${value}" but the intent never resolve and opens the fragment. – CubanAzcuy Jan 30 '21 at 02:58

0 Answers0