2

I'm trying to show a notification that when clicked will open ChatScreen. It does open ChatScreen but started as a different activity, so there's 2 MainActivity in the back stack.

I use Compose Destinations, a wrapper library for Compose Navigations.

I have tried singleTop, singleTask, singleInstance launch mode, nothing works.

ChatScreen:

@Destination(deepLinks = [DeepLink(uriPattern = "https://mantools/chats")])
@Composable
fun ChatScreen(
    navigator: DestinationsNavigator,
    viewModel: ChatViewModel = hiltViewModel()
) {
  //contents
}

Manifest

<application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Mantools"
        android:hardwareAccelerated="true"
        android:usesCleartextTraffic="${usesCleartextTraffic}"
        tools:targetApi="31">
        <activity
            android:name=".features.MainActivity"
            android:launchMode="singleInstance"
            android:exported="true"
            android:theme="@style/Theme.Mantools.Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <data android:scheme="https" android:host="mantools"/>
            </intent-filter>
        </activity>
</application>

Notification

private fun showChatNotification(message: RemoteMessage) {
        val data = message.data
        showNotification(
            data["title"] ?: "",
            data["text"] ?: "",
            TAG_CHAT,
            Intent(Intent.ACTION_MAIN, "https://mantools/chats".toUri(), this.applicationContext, MainActivity::class.java)
        )
    }


    fun showNotification(
        title: String,
        message: String,
        tag: String,
        intent: Intent
    ) {
        val pendingIntent = getPendingIntent(intent)
        val builder = NotificationCompat.Builder(context, GENERAL_CHANNEL_ID)
            .setLargeIcon(context.getDrawable(R.mipmap.ic_launcher)?.toBitmap())
            .setSmallIcon(R.drawable.app_logo)
            .setColor(ContextCompat.getColor(context, R.color.primary))
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
        notificationManager.notify(tag, System.currentTimeMillis().toInt(), builder.build())
    }
    
    private fun getPendingIntent(intent: Intent) = TaskStackBuilder.create(context).run {
        addNextIntentWithParentStack(intent)
        getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    }
  • I have the same issue. What did you end up doing to solve the problem? – LilMoke Mar 23 '23 at 13:26
  • @LilMoke see my workaround here https://github.com/raamcosta/compose-destinations/issues/106#issuecomment-1479622652 – Risal Fajar Amiyardi Mar 24 '23 at 14:25
  • Thank you for the reply. I actually did a similar workaround, but I was hoping for the 'correct' fix... maybe like how to correctly use the launch modes, if that what they are for. Wish a google person would reply with the 'correct' way of handling this, it seems like a pretty standard issue. Anyway, thank you and at least I know someone else shares the same problem... LOL – LilMoke Mar 25 '23 at 15:08

0 Answers0