CleverTap / clevertap-flutter

CleverTap Flutter SDK
Other
30 stars 42 forks source link

pushClickedPayloadReceived not being called on Android 12 and above #197

Closed zamargab closed 11 months ago

zamargab commented 11 months ago

My implementation works on Android 11 and below but for android 12 and above, i cant seem to call pushClickedPayloadReceived when the notification is clicked when app is running in background to access notification payload. when app is killed, everything works fine.

I read the docs and was told to edit my MainActivity file, below is my code

package com.example.paysure

import io.flutter.embedding.android.FlutterActivity
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import io.flutter.embedding.android.FlutterFragmentActivity

class MainActivity: FlutterFragmentActivity() {

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)

        // On Android 12 and above, inform the notification click to get the pushClickedPayloadReceived callback on dart side.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            cleverTapDefaultInstance?.pushNotificationClickedEvent(intent!!.extras)
        }
      }
}

I am getting error below. e: file:///Users/user/dev/peppa/android/app/src/main/kotlin/com/example/paysure/MainActivity.kt:13:38 Unresolved reference: Intent e: file:///Users/user/dev/peppa/android/app/src/main/kotlin/com/example/paysure/MainActivity.kt:17:13 Unresolved reference: Build e: file:///Users/user/dev/peppa/android/app/src/main/kotlin/com/example/paysure/MainActivity.kt:17:38 Unresolved reference: Build e: file:///Users/user/dev/peppa/android/app/src/main/kotlin/com/example/paysure/MainActivity.kt:18:13 Unresolved reference: cleverTapDefaultInstance

shivamsharma2710 commented 11 months ago

Hi @zamargab

It seems you didn't define the cleverTapDefaultInstance and also not imported required classes into the MainActivity. To resolve this issue, please refer the following code:

import android.content.Intent
import android.os.Build
import com.clevertap.android.sdk.CleverTapAPI
import io.flutter.embedding.android.FlutterFragmentActivity

class MainActivity: FlutterFragmentActivity() {

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)

        // On Android 12 and above, inform the notification click to get the above mentioned callback on dart side.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val cleverTapDefaultInstance = CleverTapAPI.getDefaultInstance(this);
            cleverTapDefaultInstance?.pushNotificationClickedEvent(intent.extras)
        }
    }
}
shivamsharma2710 commented 11 months ago

Closing this ticket for now. Feel free to reach out, if you need further assistance.