Search code examples
androidpush-notificationnotificationsandroid-pendingintent

Notification PendingIntent not working when app is in background state


UPDATE!!!

I solved this problem. The problem was with remoteMessage.notification.let { which is triggered only when app is in foreground state, thus I removed this part and it worked like a charm)

When I send push notification and when app is in background state notification is coming but whem I click it is not doing anything. But when app is in foreground, the click is taking to an app again.

Here is some code from FirebaseMessagingService

remoteMessage.notification.let { val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        val intent = Intent(this, MainActivity::class.java)
                .putExtra(CLICK_ACTION, mapData.get("click_action"))
                .putExtra(PAGE_TO_GO, mapData.get("pagetogo"))
                .putExtra(NOTIFIATION_TITLE, mapData.get("title"))
                .putExtra(NOTIFICATION_BODY, mapData.get("body"))
                .putExtra(COMPANY_ID, mapData.get("company_id"))
                .putExtra(DOCUMENT_ID, mapData.get("document_id"))
                .putExtra(ORDER_ID, mapData.get("order_id"))
                .putExtra(FROM_DATE_EXTRA, mapData.get("from_date"))
                .putExtra(TO_DATE_EXTRA, mapData.get("to_date"))
                .putExtra(LEDGER_VIEW_ID_EXTRA, mapData.get("id_for_ledge_view"))
                .putExtra(IS_CREDIT_EXTRA, mapData.get("is_credit"))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

        val pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val notification = NotificationCompat.Builder(this, getString(R.string.channel_id))
            .setSmallIcon(R.drawable.ic_baseline_notifications_24)
            .setContentTitle(it?.title)
            .setContentText(it?.body)
            .setContentIntent(pendingIntent)
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText(it?.body)
            )
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
        with(notificationManager) {
            notify(NOTIFICATION_ID, notification.build())
        }

Solution

  • I solved this problem. The problem was with remoteMessage.notification.let { which is triggered only when app is in foreground state, thus I removed this part and it worked like a charm)