@@ -23,9 +23,12 @@ package com.leanplum.migration.wrapper
2323
2424import android.app.Application
2525import android.content.Context
26+ import android.text.TextUtils
2627import com.clevertap.android.sdk.ActivityLifecycleCallback
2728import com.clevertap.android.sdk.CleverTapAPI
2829import com.clevertap.android.sdk.CleverTapInstanceConfig
30+ import com.clevertap.android.sdk.pushnotification.PushConstants
31+ import com.clevertap.android.sdk.pushnotification.PushNotificationHandler
2932import com.leanplum.callbacks.CleverTapInstanceCallback
3033import com.leanplum.internal.Constants
3134import com.leanplum.internal.Log
@@ -34,6 +37,7 @@ import com.leanplum.migration.MigrationManager
3437import com.leanplum.migration.push.FcmMigrationHandler
3538import com.leanplum.migration.push.HmsMigrationHandler
3639import com.leanplum.migration.push.MiPushMigrationHandler
40+ import com.leanplum.utils.SharedPreferencesUtil
3741
3842internal class CTWrapper (
3943 private val accountId : String ,
@@ -50,6 +54,7 @@ internal class CTWrapper(
5054 private var cleverTapInstance: CleverTapAPI ? = null
5155 private var instanceCallback: CleverTapInstanceCallback ? = null
5256
57+ private var firstTimeStart = IdentityManager .isStateUndefined()
5358 private var identityManager = IdentityManager (deviceId, userId ? : deviceId)
5459
5560 override fun launch (context : Context , callback : CleverTapInstanceCallback ? ) {
@@ -85,6 +90,10 @@ internal class CTWrapper(
8590 }
8691 Log .d(" Wrapper: CleverTap instance created by Leanplum" )
8792 }
93+ if (firstTimeStart) {
94+ // Send tokens in same session, because often a restart is needed for CT SDK to get them
95+ sendPushTokens(context)
96+ }
8897 triggerInstanceCallback()
8998 }
9099
@@ -102,6 +111,35 @@ internal class CTWrapper(
102111 triggerInstanceCallback()
103112 }
104113
114+ private fun sendPushTokens (context : Context ) {
115+ // FCM
116+ val fcmToken = SharedPreferencesUtil .getString(context,
117+ Constants .Defaults .LEANPLUM_PUSH , Constants .Defaults .PROPERTY_FCM_TOKEN_ID )
118+ if (! TextUtils .isEmpty(fcmToken)) {
119+ val type = PushConstants .PushType .FCM .type
120+ PushNotificationHandler .getPushNotificationHandler().onNewToken(context, fcmToken, type)
121+ Log .d(" Wrapper: fcm token sent" )
122+ }
123+
124+ // XPS
125+ val miPushToken = SharedPreferencesUtil .getString(context,
126+ Constants .Defaults .LEANPLUM_PUSH , Constants .Defaults .PROPERTY_MIPUSH_TOKEN_ID )
127+ if (! TextUtils .isEmpty(miPushToken)) {
128+ val type = PushConstants .PushType .XPS .type
129+ PushNotificationHandler .getPushNotificationHandler().onNewToken(context, miPushToken, type)
130+ Log .d(" Wrapper: xps token sent" )
131+ }
132+
133+ // HMS
134+ val hmsToken = SharedPreferencesUtil .getString(context,
135+ Constants .Defaults .LEANPLUM_PUSH , Constants .Defaults .PROPERTY_HMS_TOKEN_ID )
136+ if (! TextUtils .isEmpty(hmsToken)) {
137+ val type = PushConstants .PushType .HPS .type
138+ PushNotificationHandler .getPushNotificationHandler().onNewToken(context, hmsToken, type)
139+ Log .d(" Wrapper: hms token sent" )
140+ }
141+ }
142+
105143 override fun setUserId (userId : String? ) {
106144 if (userId == null || userId.isEmpty()) return
107145 if (identityManager.getUserId() == userId) return
0 commit comments