0

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())
    }

matdev
  • 4,115
  • 6
  • 35
  • 56
  • 1
    You can check this https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) and this may also help https://stackoverflow.com/questions/3168484/pendingintent-works-correctly-for-the-first-notification-but-incorrectly-for-the – Rahul Shukla Nov 29 '19 at 09:02
  • Your code doesn't show how you create the `Intent` that you pass in to `createNotification()`. That's actually the interesting bit. – David Wasser Dec 02 '19 at 21:45

1 Answers1

0

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
            )
  • Generally speaking, I wouldn't use `FLAG_ONE_SHOT`. This has a lot of nasty side-effects and isn't necessary. If you set a unique ACTION (which you are doing), then you will get unique `PendingIntent`s (which is what you need) without the flag. See https://stackoverflow.com/questions/29778294/pending-intent-with-one-shot-flag/29780579#29780579 for more info – David Wasser Dec 04 '19 at 11:47