与初始屏幕和登录状态的深度链接



我使用导航组件。我的应用程序有两个主要部分:1 次飞溅活动,nav_graph2 具有底部导航的主要活动,其中每个菜单项都有自己的nav_graph。问题是当弹出通知时,我无法弄清楚如何进行导航。

因此,当通知到达并且用户单击它时,我需要:- 检查应用程序是否打开以及现在打开的屏幕- 如果应用程序已关闭或在后台,通过深层链接,在启动活动时检查:如果用户已登录 - 移动到主活动(如果未提供身份验证屏幕(保持导航深层链接到我需要的片段。

启动图和三个 memu 项目图之一

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/splash_graph"
            app:startDestination="@id/splashFragment">
    <fragment android:id="@+id/splashFragment"
              android:name="com.app.app.ui.navigation.fragment.SplashFragment"
              android:label="SplashFragment">
        <action android:id="@+id/action_splashFragment_to_authFragment"
                app:popUpTo="@+id/authFragment"
                app:popUpToInclusive="true"
                app:destination="@id/authFragment"/>
        <action android:id="@+id/action_splashFragment_to_mainActivity"
                app:popUpTo="@+id/mainActivity"
                app:popUpToInclusive="true"
                app:destination="@id/mainActivity" app:launchSingleTop="true"/>
    </fragment>
    <fragment android:id="@+id/authFragment"
              android:name="com.app.app.ui.navigation.fragment.AuthFragment"
              android:label="AuthFragment">
        <action android:id="@+id/action_authFragment_to_mainActivity"
                app:popUpTo="@+id/mainActivity"
                app:popUpToInclusive="true"
                app:destination="@id/mainActivity" app:launchSingleTop="true"/>
    </fragment>
    <activity android:id="@+id/mainActivity"
              android:name="com.app.app.ui.navigation.activity.MainActivity"
              android:label="MainActivity">
        <deepLink android:id="@+id/deepLinkMain" app:uri="com.app.app/"/>
    </activity>

</navigation>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/nextFragment"
            app:startDestination="@id/nxtFragment">
    <fragment android:id="@+id/nxtFragment"
              android:name="com.app.app.ui.navigation.fragment.NextFragment"
              android:label="Next">
        <deepLink
                android:id="@+id/deepLink"
                app:uri="com.app.app/nextFragment{id}"
                android:autoVerify="true"/>
        <argument
                android:name="id"
                app:argType="string"/>
    </fragment>
</navigation>

您可以使用挂起的意图来执行此操作,假设您获得如下所示的数据有效负载:

{
      "title":"Title",
      "message":"message test",
      "data":{
             "meta":{
                "notificationType":"1",
                 "name":"Rjz"
                    }
             }
}

根据通知类型,您必须打开特定屏幕,然后您可以执行以下操作:

sendNotification("msg","title",remoteMessage.data(

private fun sendNotification(messageBody: String,title: String?,data: MutableMap<String, String>) {
    val intent = Intent(this, SplashActivity::class.java)
    intent.putExtra("meta", data["meta"])
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(
        this, 0 /* Request code */, intent,
        PendingIntent.FLAG_ONE_SHOT
    )
    val channelId = getString(R.string.default_notification_channel_id)
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)
    notificationBuilder.setSmallIcon(R.drawable.ic_app_logo)
        .setContentTitle(title)
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setStyle(NotificationCompat.BigTextStyle())
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent)
    val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            channelId,
            "Channel human readable title",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        notificationManager.createNotificationChannel(channel)
    }
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}

在启动画面中,您可以检查 待处理的意图数据 ,如下所示:

val metaString: String? = if (intent.hasExtra("meta")) {
                intent.extras.get("meta").toString()
            } else {
                null
            }
    var gson = Gson()   
    var pushData = gson.fromJson(
                    metaString,
                    YourModel::class.java
                )       
    if(pushData.notificationType==1){
    NavDeepLinkBuilder(this@SplashActivity)
            .setComponentName(MainActivity::class.java)
            .setGraph(R.navigation.name_of_nav_graph)
            .setDestination(R.id.your_dstination_fragment)
            .setArguments(bundleOf(
                         "name" to pushData.name)
                         ).createTaskStackBuilder().startActivities()
    }

如果要传递,也可以传递参数。

在这种情况下,这是一种简单的方法。如果你卡在任何地方,请告诉我。享受。。!!

相关内容

  • 没有找到相关文章

最新更新