CleverTap / clevertap-android-sdk

CleverTap Android SDK
MIT License
80 stars 75 forks source link

Android push notification click is not detected on Android 12 and above #693

Open thecode007 opened 3 days ago

thecode007 commented 3 days ago

Describe the bug Android push notification click is not detected on Android 12 and above when:

AnApp: class AnApp: Application() { override fun onCreate() { CleverTapProxy.initialize(this) super.onCreate() } }

CleverTapProxy:

class CleverTapProxy private constructor(context: Context) : Application.ActivityLifecycleCallbacks {

    private var cleverTapAPI: CleverTapAPI? = null

    init {
        cleverTapAPI = CleverTapAPI.getDefaultInstance(context)
        Log.wtf("Clever Tap id --->", cleverTapAPI?.cleverTapID)
        FirebaseAnalytics.getInstance(context)
            .setUserProperty("ct_objectId", cleverTapAPI?.cleverTapID)
    }

    companion object {

        private var cleverTapManager: CleverTapProxy? = null

        fun initialize(context: Application) {
            if (cleverTapManager == null) {
                cleverTapManager = CleverTapProxy(context)
                cleverTapManager?.cleverTapAPI?.enableDeviceNetworkInfoReporting(true)
                CleverTapAPI.createNotificationChannel(
                    context,
                    context.getString(R.string.default_channel_id),
                    context.getString(R.string.default_channel_name),
                    "CLEVERTAP",
                    NotificationManager.IMPORTANCE_MAX,
                    true
                )
                context.registerActivityLifecycleCallbacks(cleverTapManager)
            }
        }

        fun getInstance(): CleverTapProxy = cleverTapManager!!
        private fun registerFMToken(token: String) {
        cleverTapAPI?.pushFcmRegistrationId(token, true)
    }
}

NotificationsService:


class NotificationsService :
    FirebaseMessagingService(),
    CTPushNotificationListener {

    @Inject
    lateinit var prefsStore: PrefsStore

    companion object {
        private lateinit var notificationManager: NotificationManager
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        CoroutineScope(Dispatchers.IO).launch {
            prefsStore.setFCMToken(token)
        }
        CleverTapProxy.registerFCMToken(token)
        (application as EsimApp).saveFCMToken()
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        val channelId = remoteMessage.data["channel_id"] ?: "default_channel"

        try {
            logRemoteMessage(remoteMessage)
            processRemoteMessage(remoteMessage, channelId)
        } catch (_: Exception) {
        }
    }

    private fun logRemoteMessage(remoteMessage: RemoteMessage) {
        remoteMessage.notification?.toString()?.let { Log.i("FCM_Noti", it) }
    }

    private fun processRemoteMessage(remoteMessage: RemoteMessage, channelId: String) {
        val category = remoteMessage.data[CATEGORY]?.toIntOrNull()
        try {
            val handled = CTFcmMessageHandler()
                .createNotification(applicationContext, remoteMessage)
            if (handled) {
                val extras = Bundle()
                for ((key, value) in remoteMessage.data) {
                    extras.putString(key, value)
                }
                CleverTapAPI.getDefaultInstance(this)?.ctPushNotificationListener = this
                CleverTapAPI.getDefaultInstance(this)?.pushNotificationViewedEvent(extras)
                return
            }
        } catch (t: Throwable) {
            Log.d("MYFCMLIST", "Error parsing FCM message", t)
        }
//            return
//        }

    }

    private fun sendNotification(notification: String?, title: String?, channelId: String, pendingIntent: PendingIntent? = null) {
        if (notification != null && title != null) {
            notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

            val intent = packageManager.getLaunchIntentForPackage(packageName)
            val defaultPendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

            val notificationBuilder = NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notification_small)
                .setContentTitle(title)
                .setStyle(NotificationCompat.BigTextStyle().bigText(notification))
                .setContentText(notification)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent ?: defaultPendingIntent)

            notificationManager.notify(
                System.currentTimeMillis().toInt(),
                notificationBuilder.build()
            )
        }
    }

    override fun onNotificationClickedPayloadReceived(payload: HashMap<String, Any>?) {
        val stackBuilder = TaskStackBuilder.create(this)
        stackBuilder.addNextIntent(
            Intent(this, OnBoardingActivity::class.java).apply {
                putExtra("push", payload?.get("wzrk_dl") as String)
            }
        )
        stackBuilder.startActivities()
    }
}

AndroidManifest:

  <receiver
            android:name="com.clevertap.android.sdk.pushnotification.CTPushNotificationReceiver"
            android:enabled="true"
            android:exported="false" />

 <service
            android:name=".services.NotificationsService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
 <meta-data
            android:name="CLEVERTAP_ACCOUNT_ID"
            android:value="@string/clevertap_id" />
<meta-data
            android:name="CLEVERTAP_TOKEN"
            android:value="@string/clevertap_token" />

Environment (please complete the following information):

piyush-kukadiya commented 3 days ago

@thecode007 Did you go through this? https://developer.clevertap.com/docs/android-12-updates#notification-trampolines

thecode007 commented 3 days ago

@thecode007 Did you go through this? https://developer.clevertap.com/docs/android-12-updates#notification-trampolines Yes I did