Search code examples
androidkotlinandroid-intentandroid-notificationsandroid-pendingintent

subsequent notifications does not reload the Main Activity


My App creates a notification and when the user taps on the notification, it opens the Main Activity with some EXTRA data.

Everything works fine when the user opens the App manually or the first time the notification pops up.

But the problem is that, when the App is first opened by the notification, subsequent notifications does not reload the Main Activity. It only show it(without reload) if the App is hidden.

This is how I create the notification


fun createNotification(title: String, message: String, intent: Intent?) {
        val mBuilder = NotificationCompat.Builder(
            context,
            NOTIFICATION_CHANNEL_ID
        )
        mBuilder.setSmallIcon(R.drawable.notification_ico)
        mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)

        if (intent != null) {
            Log.e(TAG, "Intent dey")
            val resultPendingIntent = PendingIntent.getActivity(
                context,
                0 /* Request code */,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            mBuilder.setContentIntent(resultPendingIntent)
        } else {
            Log.e(TAG, "Unavailable dey")
        }

        val mNotificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel = NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                "Enterprise Advantage",
                importance
            )
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.RED
            notificationChannel.enableVibration(true)
            notificationChannel.vibrationPattern =
                longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
            mNotificationManager.createNotificationChannel(notificationChannel)
        }
        mNotificationManager.notify(0 /* Request Code */, mBuilder.build())
    }


Solution

  • Okay so from the comment by @Rahul Shukla,

    I set an action on the intent

    val intent = Intent(this, MainActivity::class.java)
    intent.action = System.currentTimeMillis().toString()
    

    and then changed the Flag of the pending intent to PendingIntent.FLAG_ONE_SHOT

    val resultPendingIntent = PendingIntent.getActivity(
                    context,
                    0 /* Request code */,
                    intent,
                    PendingIntent.FLAG_ONE_SHOT
                )