Push notifications not working in android 12 after following behavior - changes guide

378 Views Asked by At

After updating targeted SDK level 31 from 30 notification getting genarate but on click of notification nothing happens.

We are not able to start activity from service class. Document mentions below two solutions but that didn't work

Update your app

If your app starts an activity from a service or broadcast receiver that acts as a notification trampoline, complete the following migration steps:

Create a PendingIntent object that is associated with the activity that users see after they tap on the notification. Use the PendingIntent object that you created in the previous step as part of building your notification.

https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines

I have tried below code but didn't worked

Class PushMessagingService:FirebaseMessagingService() {

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

val notifyIntent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val notifyPendingIntent = PendingIntent.getActivity(
            this,
            1,
            notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val builder = NotificationCompat.Builder(this, "CHANNEL_ID").apply {
            setSmallIcon(R.drawable.logo)
            setContentTitle(getString(R.string.app_name))
            setContentText(getString(R.string.logging_in))
            priority = NotificationCompat.PRIORITY_HIGH
            setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
            setContentIntent(notifyPendingIntent)
            setAutoCancel(true)
        }
        val c = AtomicInteger(0)
        val notificationId = c.incrementAndGet()
        with(NotificationManagerCompat.from(this)) {
            notify(notificationId, builder.build())
        }
}
}

how can we open expected activity?

0

There are 0 best solutions below