0

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?

  • I have tested your code and it worked fine: Notification came up, I clicked on it and the MainActivity was launched. Perhaps there's something else you're forgetting to do? You can also try running the [FCM quickstart sample app](https://github.com/firebase/quickstart-android/tree/master/messaging) and see if the same problem occurs. – Rosário Pereira Fernandes Feb 02 '23 at 17:20
  • 1
    @RosárioPereiraFernandes Thank You! I have referred your sample code and its working as expected. – Swapnil Akubattin Feb 07 '23 at 05:51

0 Answers0