按下通知时将 Android 应用程序置于前台

问题描述 投票:0回答:1

我在应用程序的某个位置启动服务并从那里创建通知。我将应用程序置于后台并单击通知

我想要发生的事情:应用程序以与原来相同的状态进入前台,就像我按下设备上的“查看所有后台应用程序”按钮并选择它一样。

实际发生的情况:我进入应用程序的主屏幕。

我梳理了一堆线索并尝试了不同的事情。每次我更改任何内容时,当我按下通知时,应用程序根本不会打开。

class TestService : Service() {
    private lateinit var notificationBuilder: NotificationCompat.Builder

    ...

    private fun createNotification(): Notification {
        ...
        notificationBuilder = NotificationCompat.Builder(this, channelId)

        notificationBuilder
            .setSmallIcon(R.drawable.ic_notif)
            .setContentTitle(titleText)
            .setContentText(getFormattedTime())
            .setOnlyAlertOnce(true)
            .setContentIntent(PendingIntent.getActivity(this, 1, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE))
            .setOngoing(true)

        ...

        return notificationBuilder.build()
    }

android android-intent android-notifications
1个回答
0
投票

您只需要使用 launch

Intent
代替,如下所示:

val intent = getPackageManager().
                getLaunchIntentForPackage("my.package.name")

然后

.setContentIntent(PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_IMMUTABLE))
        
© www.soinside.com 2019 - 2024. All rights reserved.