当单击推送通知时,在导航图中打开特定的片段目标,并且图形不在启动器活动中

问题描述

用户单击推送通知时,我想在导航组件图中打开特定片段。 但是我不止一个导航图。我要浏览的目标片段不在启动器活动图中。

val pendingIntent = NavDeepLinkBuilder(this)
            .setGraph(R.navigation.nav_home)
            .setDestination(R.id.detailsFragment)
            .setArguments(args)
            .createPendingIntent()

        val notificationBuilder = NotificationCompat.Builder(
            this,getChannelIDForOnGoingNotification(this)
        ).setContentTitle(remoteMessage?.data?.get("title"))
            .setSmallIcon(getNotificationIcon())
            .setColor(getNotificationColor())
            .setContentText(remoteMessage?.data?.get("body"))
            .setPriority(notificationmanager.IMPORTANCE_HIGH)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setStyle(bigText)
            .setContentIntent(pendingIntent)

        val notificationmanager = getSystemService(Context.NOTIFICATION_SERVICE) as notificationmanager

        notificationmanager.notify(notificationId,notificationBuilder.build()) 

我正在使用上面的代码使用NavDeepLinkBuilder进行深层链接。但是R.navigation.nav_home不在启动器活动中。当我单击通知时,它只是打开启动器活动,而不打开目标活动和片段。

如何使用Android导航组件打开包含具有目标片段的图形的目标活动

解决方法

您必须添加

.setComponentName(MainActivity::class.java)

您的代码应如下所示:

    val pendingIntent = NavDeepLinkBuilder(this)
        .setComponentName(MainActivity::class.java) // your destination activity
        .setGraph(R.navigation.nav_home)
        .setDestination(R.id.detailsFragment)
        .setArguments(args)
        .createPendingIntent()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...