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.