@@ -10,6 +10,7 @@ import com.braze.BrazeActivityLifecycleCallbackListener
1010import com.braze.BrazeUser
1111import com.braze.configuration.BrazeConfig
1212import com.braze.enums.*
13+ import com.braze.events.IValueCallback
1314import com.braze.models.outgoing.BrazeProperties
1415import com.braze.push.BrazeFirebaseMessagingService
1516import com.braze.push.BrazeNotificationUtils.isBrazePushMessage
@@ -131,27 +132,38 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
131132 Braze .getInstance(context).logCustomEvent(event.eventName)
132133 } else {
133134 val properties = BrazeProperties ()
134- val user = Braze .getInstance(context).currentUser
135135 val brazePropertiesSetter = BrazePropertiesSetter (properties, enableTypeDetection)
136- val userAttributeSetter = user?.let { UserAttributeSetter (it, enableTypeDetection) }
137136 event.customAttributeStrings?.let { it ->
138137 for ((key, value) in it) {
139138 newAttributes[key] = brazePropertiesSetter.parseValue(key, value)
140- val hashedKey =
141- KitUtils .hashForFiltering(event.eventType.toString() + event.eventName + key)
142-
143- configuration.eventAttributesAddToUser?.get(hashedKey)?.let {
144- user?.addToCustomAttributeArray(it, value)
145- }
146- configuration.eventAttributesRemoveFromUser?.get(hashedKey)?.let {
147- user?.removeFromCustomAttributeArray(it, value)
148- }
149- configuration.eventAttributesSingleItemUser?.get(hashedKey)?.let {
150- userAttributeSetter?.parseValue(it, value)
151- }
152139 }
153140 }
154141 Braze .getInstance(context).logCustomEvent(event.eventName, properties)
142+ Braze .getInstance(context).getCurrentUser(object : IValueCallback <BrazeUser > {
143+ override fun onSuccess (value : BrazeUser ) {
144+ val userAttributeSetter = UserAttributeSetter (value, enableTypeDetection)
145+ event.customAttributeStrings?.let { it ->
146+ for ((key, attributeValue) in it) {
147+ val hashedKey =
148+ KitUtils .hashForFiltering(event.eventType.toString() + event.eventName + key)
149+
150+ configuration.eventAttributesAddToUser?.get(hashedKey)?.let {
151+ value.addToCustomAttributeArray(it, attributeValue)
152+ }
153+ configuration.eventAttributesRemoveFromUser?.get(hashedKey)?.let {
154+ value.removeFromCustomAttributeArray(it, attributeValue)
155+ }
156+ configuration.eventAttributesSingleItemUser?.get(hashedKey)?.let {
157+ userAttributeSetter.parseValue(it, attributeValue)
158+ }
159+ }
160+ }
161+ }
162+
163+ override fun onError () {
164+ Logger .warning(" unable to acquire user to add or remove custom user attributes from events" )
165+ }
166+ })
155167 }
156168 queueDataFlush()
157169 return listOf (ReportingMessage .fromEvent(this , event).setAttributes(newAttributes))
@@ -237,61 +249,66 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
237249 return messages
238250 }
239251
240- override fun setUserAttribute (keyIn : String , value : String ) {
252+ override fun setUserAttribute (keyIn : String , attributeValue : String ) {
241253 var key = keyIn
242- val user = Braze .getInstance(context).currentUser
243- val userAttributeSetter = user?.let { UserAttributeSetter (it, enableTypeDetection) }
244-
245- user?.apply {
246- when (key) {
247- UserAttributes .CITY -> setHomeCity(value)
248- UserAttributes .COUNTRY -> setCountry(value)
249- UserAttributes .FIRSTNAME -> setFirstName(value)
250- UserAttributes .LASTNAME -> setLastName(value)
251- UserAttributes .MOBILE_NUMBER -> setPhoneNumber(value)
252- UserAttributes .ZIPCODE -> setCustomUserAttribute(" Zip" , value)
253- UserAttributes .AGE -> {
254- val calendar = getCalendarMinusYears(value)
255- if (calendar != null ) {
256- user.setDateOfBirth(calendar[Calendar .YEAR ], Month .JANUARY , 1 )
257- } else {
258- Logger .warning(" unable to set DateOfBirth for " + UserAttributes .AGE + " = " + value)
254+ Braze .getInstance(context).getCurrentUser(object : IValueCallback <BrazeUser > {
255+ override fun onSuccess (value : BrazeUser ) {
256+ val userAttributeSetter = UserAttributeSetter (value, enableTypeDetection)
257+
258+ when (key) {
259+ UserAttributes .CITY -> value.setHomeCity(attributeValue)
260+ UserAttributes .COUNTRY -> value.setCountry(attributeValue)
261+ UserAttributes .FIRSTNAME -> value.setFirstName(attributeValue)
262+ UserAttributes .LASTNAME -> value.setLastName(attributeValue)
263+ UserAttributes .MOBILE_NUMBER -> value.setPhoneNumber(attributeValue)
264+ UserAttributes .ZIPCODE -> value.setCustomUserAttribute(" Zip" , attributeValue)
265+ UserAttributes .AGE -> {
266+ val calendar = getCalendarMinusYears(attributeValue)
267+ if (calendar != null ) {
268+ value.setDateOfBirth(calendar[Calendar .YEAR ], Month .JANUARY , 1 )
269+ } else {
270+ Logger .warning(" unable to set DateOfBirth for " + UserAttributes .AGE + " = " + value)
271+ }
259272 }
260- }
261- EMAIL_SUBSCRIBE -> {
262- when ( value) {
263- OPTED_IN -> user .setEmailNotificationSubscriptionType(NotificationSubscriptionType .OPTED_IN )
264- UNSUBSCRIBED -> user .setEmailNotificationSubscriptionType(NotificationSubscriptionType .UNSUBSCRIBED )
265- SUBSCRIBED -> user.setEmailNotificationSubscriptionType( NotificationSubscriptionType . SUBSCRIBED )
266- else -> {
267- Logger .warning( " unable to set email_subscribe with invalid value: " + value)
273+ EMAIL_SUBSCRIBE -> {
274+ when (attributeValue) {
275+ OPTED_IN -> value.setEmailNotificationSubscriptionType( NotificationSubscriptionType . OPTED_IN )
276+ UNSUBSCRIBED -> value .setEmailNotificationSubscriptionType(NotificationSubscriptionType .UNSUBSCRIBED )
277+ SUBSCRIBED -> value .setEmailNotificationSubscriptionType(NotificationSubscriptionType .SUBSCRIBED )
278+ else -> {
279+ Logger .warning( " unable to set email_subscribe with invalid value: " + value)
280+ }
268281 }
269282 }
270- }
271- PUSH_SUBSCRIBE -> {
272- when ( value) {
273- OPTED_IN -> user .setPushNotificationSubscriptionType(NotificationSubscriptionType .OPTED_IN )
274- UNSUBSCRIBED -> user .setPushNotificationSubscriptionType(NotificationSubscriptionType .UNSUBSCRIBED )
275- SUBSCRIBED -> user.setPushNotificationSubscriptionType( NotificationSubscriptionType . SUBSCRIBED )
276- else -> {
277- Logger .warning( " unable to set push_subscribe with invalid value: " + value)
283+ PUSH_SUBSCRIBE -> {
284+ when (attributeValue) {
285+ OPTED_IN -> value.setPushNotificationSubscriptionType( NotificationSubscriptionType . OPTED_IN )
286+ UNSUBSCRIBED -> value .setPushNotificationSubscriptionType(NotificationSubscriptionType .UNSUBSCRIBED )
287+ SUBSCRIBED -> value .setPushNotificationSubscriptionType(NotificationSubscriptionType .SUBSCRIBED )
288+ else -> {
289+ Logger .warning( " unable to set push_subscribe with invalid value: " + value)
290+ }
278291 }
279292 }
280- }
281- DOB -> useDobString(value, user)
282- UserAttributes .GENDER -> {
283- if (value.contains(" fe" )) setGender(Gender .FEMALE )
284- else setGender(Gender .MALE )
285- }
286- else -> {
287- if (key.startsWith(" $" )) {
288- key = key.substring(1 )
293+ DOB -> useDobString(attributeValue, value)
294+ UserAttributes .GENDER -> {
295+ if (attributeValue.contains(" fe" )) value.setGender(Gender .FEMALE )
296+ else value.setGender(Gender .MALE )
297+ }
298+ else -> {
299+ if (key.startsWith(" $" )) {
300+ key = key.substring(1 )
301+ }
302+ userAttributeSetter?.parseValue(key, attributeValue)
289303 }
290- userAttributeSetter?.parseValue(key, value)
291304 }
305+ queueDataFlush()
292306 }
293- }
294- queueDataFlush()
307+
308+ override fun onError () {
309+ Logger .warning(" unable to set key: " + key + " with value: " + attributeValue)
310+ }
311+ })
295312 }
296313
297314 // Expected Date Format @"yyyy'-'MM'-'dd"
@@ -311,10 +328,17 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
311328 }
312329
313330 override fun setUserAttributeList (key : String , list : List <String >) {
314- val user = Braze .getInstance(context).currentUser
315- val array = list.toTypedArray<String ?>()
316- user?.setCustomAttributeArray(key, array)
317- queueDataFlush()
331+ Braze .getInstance(context).getCurrentUser(object : IValueCallback <BrazeUser > {
332+ override fun onSuccess (value : BrazeUser ) {
333+ val array = list.toTypedArray<String ?>()
334+ value.setCustomAttributeArray(key, array)
335+ queueDataFlush()
336+ }
337+
338+ override fun onError () {
339+ Logger .warning(" unable to set key: " + key + " with User Attribute List: " + list)
340+ }
341+ })
318342 }
319343
320344 override fun supportsAttributeLists (): Boolean = false
@@ -344,27 +368,33 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
344368
345369 override fun removeUserAttribute (keyIn : String ) {
346370 var key = keyIn
347- val user = Braze .getInstance(context).currentUser
348-
349- if (UserAttributes .CITY == key) {
350- user?.setHomeCity(null )
351- } else if (UserAttributes .COUNTRY == key) {
352- user?.setCountry(null )
353- } else if (UserAttributes .FIRSTNAME == key) {
354- user?.setFirstName(null )
355- } // else if (UserAttributes.GENDER == key) { //Braze SDK wont allow for gender parameter to be null.
356- // user.setGender(null)}
357- else if (UserAttributes .LASTNAME == key) {
358- user?.setLastName(null )
359- } else if (UserAttributes .MOBILE_NUMBER == key) {
360- user?.setPhoneNumber(null )
361- } else {
362- if (key.startsWith(" $" )) {
363- key = key.substring(1 )
371+ Braze .getInstance(context).getCurrentUser(object : IValueCallback <BrazeUser > {
372+ override fun onSuccess (value : BrazeUser ) {
373+ if (UserAttributes .CITY == key) {
374+ value.setHomeCity(null )
375+ } else if (UserAttributes .COUNTRY == key) {
376+ value.setCountry(null )
377+ } else if (UserAttributes .FIRSTNAME == key) {
378+ value.setFirstName(null )
379+ } // else if (UserAttributes.GENDER == key) { //Braze SDK wont allow for gender parameter to be null.
380+ // user.setGender(null)}
381+ else if (UserAttributes .LASTNAME == key) {
382+ value.setLastName(null )
383+ } else if (UserAttributes .MOBILE_NUMBER == key) {
384+ value.setPhoneNumber(null )
385+ } else {
386+ if (key.startsWith(" $" )) {
387+ key = key.substring(1 )
388+ }
389+ value.unsetCustomUserAttribute(key)
390+ }
391+ queueDataFlush()
364392 }
365- user?.unsetCustomUserAttribute(key)
366- }
367- queueDataFlush()
393+
394+ override fun onError () {
395+ Logger .warning(" unable to remove User Attribute with key: " + key)
396+ }
397+ })
368398 }
369399
370400 override fun setUserIdentity (identityType : IdentityType , identity : String ) {}
@@ -597,19 +627,33 @@ open class AppboyKit : KitIntegration(), AttributeListener, CommerceListener,
597627 }
598628
599629 protected open fun setId (customerId : String ) {
600- val user = Braze .getInstance(context).currentUser
601- if (user == null || user.userId != customerId) {
602- Braze .getInstance(context).changeUser(customerId)
603- queueDataFlush()
604- }
630+ Braze .getInstance(context).getCurrentUser(object : IValueCallback <BrazeUser > {
631+ override fun onSuccess (value : BrazeUser ) {
632+ if (value.userId != customerId) {
633+ Braze .getInstance(context).changeUser(customerId)
634+ queueDataFlush()
635+ }
636+ }
637+
638+ override fun onError () {
639+ Logger .warning(" unable to change user to customer ID: " + customerId)
640+ }
641+ })
605642 }
606643
607644 protected open fun setEmail (email : String ) {
608645 if (email != kitPreferences.getString(PREF_KEY_CURRENT_EMAIL , null )) {
609- val user = Braze .getInstance(context).currentUser
610- user?.setEmail(email)
611- queueDataFlush()
612- kitPreferences.edit().putString(PREF_KEY_CURRENT_EMAIL , email).apply ()
646+ Braze .getInstance(context).getCurrentUser(object : IValueCallback <BrazeUser > {
647+ override fun onSuccess (value : BrazeUser ) {
648+ value.setEmail(email)
649+ queueDataFlush()
650+ kitPreferences.edit().putString(PREF_KEY_CURRENT_EMAIL , email).apply ()
651+ }
652+
653+ override fun onError () {
654+ Logger .warning(" unable to set email with value: " + email)
655+ }
656+ })
613657 }
614658 }
615659
0 commit comments