Skip to content

Commit d2954ae

Browse files
authored
Send push tokens in first wrapper start (#520)
1 parent de0a425 commit d2954ae

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

AndroidSDKCore/src/main/java/com/leanplum/migration/wrapper/CTWrapper.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ package com.leanplum.migration.wrapper
2323

2424
import android.app.Application
2525
import android.content.Context
26+
import android.text.TextUtils
2627
import com.clevertap.android.sdk.ActivityLifecycleCallback
2728
import com.clevertap.android.sdk.CleverTapAPI
2829
import com.clevertap.android.sdk.CleverTapInstanceConfig
30+
import com.clevertap.android.sdk.pushnotification.PushConstants
31+
import com.clevertap.android.sdk.pushnotification.PushNotificationHandler
2932
import com.leanplum.callbacks.CleverTapInstanceCallback
3033
import com.leanplum.internal.Constants
3134
import com.leanplum.internal.Log
@@ -34,6 +37,7 @@ import com.leanplum.migration.MigrationManager
3437
import com.leanplum.migration.push.FcmMigrationHandler
3538
import com.leanplum.migration.push.HmsMigrationHandler
3639
import com.leanplum.migration.push.MiPushMigrationHandler
40+
import com.leanplum.utils.SharedPreferencesUtil
3741

3842
internal 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

AndroidSDKCore/src/main/java/com/leanplum/migration/wrapper/IdentityManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ internal class IdentityManager(
6565

6666
private var anonymousMergeUserId: String? by StringPreferenceNullable("ct_anon_merge_userid")
6767
private var state: String by StringPreference("ct_login_state", UNDEFINED)
68+
fun isStateUndefined() = state == UNDEFINED
6869
}
6970

7071
init {

0 commit comments

Comments
 (0)