与闪屏和登录状态的深度链接

Віт*_*мко 10 navigation android splash-screen android-architecture-navigation

我使用导航组件。我的应用程序有 2 个主要部分:1 个带有 nav_graph 的初始活动 2 个带有底部导航的主要活动,其中每个菜单项都有自己的 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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

Rjz*_*ara 4

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

{
      "title":"Title",
      "message":"message test",
      "data":{
             "meta":{
                "notificationType":"1",
                 "name":"Rjz"
                    }
             }
}
Run Code Online (Sandbox Code Playgroud)

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

sendNotification("消息","标题",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())
}
Run Code Online (Sandbox Code Playgroud)

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

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()
    }
Run Code Online (Sandbox Code Playgroud)

如果你想通过也可以通过参数。

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