I am trying a simple example of triggering a notification from an Android app and want the existing activity to be launched when the user taps on the notification. However, I see that a new instance of activity is created. What am I missing?
Application:
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)
}
}
}
Notification trigger:
@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())
}
}
MainActivity:
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:
<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>
The issue must have been due to one of the developer settings. Disabled developer options and re-enabled and I do not see the problem anymore. Unfortunately, I could not narrow down the specific setting that may have caused this issue.