即使在 singleTop 启动模式下,Android 通知点击也会启动新的 Activity 实例

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

我正在尝试一个从 Android 应用程序触发通知的简单示例,并希望当用户点击通知时启动 existing 活动。但是,我看到创建了一个新的活动实例。我错过了什么?

应用:

const val CHANNEL_ID = "100"
const val CHANNEL_NAME = "My Channel"

class HelloNotificationApp: Application() {
    override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance).apply {
                description = "Channel Description"
            }
            val notificationManager: NotificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(channel)
        }
    }
}

通知触发:

@Composable
fun NotificationScreen() {
    val context = LocalContext.current
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center,
    ) {
        Button(
            onClick = { addNotification(context) }
        ) {
            Text(text = "Send notification")
        }
    }
}

private fun addNotification(context: Context) {
    val pendingIntent = PendingIntent.getActivity(
        context,
        0,
        Intent(context, MainActivity::class.java).apply {
            putExtra("Identifier", 9999)
        },
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

    val builder = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_dialog_alert)
        .setContentTitle("My Notification")
        .setContentText("Hello, this is a notification!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)

    with(NotificationManagerCompat.from(context)) {
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
        ) {
            Toast.makeText(context, "Need permission", Toast.LENGTH_SHORT).show()
            return
        }
        notify(1, builder.build())
    }
}

主要活动:

class MainActivity : ComponentActivity() {
    init {
        Log.e("++++", "++++ INIT MAIN $this")
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            HelloNotificationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    NotificationScreen()
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        val id = intent.getIntExtra("Identifier", -1)
        Log.e("++++", "Identifier $id")
    }
}

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <application
        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:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HelloNotification"
        android:name=".HelloNotificationApp"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:theme="@style/Theme.HelloNotification">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
android android-activity android-manifest android-notifications android-push-notification
1个回答
0
投票

该问题肯定是由开发者设置之一造成的。禁用开发人员选项并重新启用,我不再看到问题。不幸的是,我无法缩小可能导致此问题的具体设置范围。

© www.soinside.com 2019 - 2024. All rights reserved.