Users and data¶
Users¶
The SDK works without users: a device without one belongs to an anonymous recipient (see Concepts).
Kotlin
PlingBuddy.login("user-42") // the user signed in
PlingBuddy.logout() // the user signed out
logingives the anonymous recipient the external id, or, when another recipient already has it (the user signed in on another device first), moves the device to that recipient.loginwith another user moves the device to that user's recipient, and forgets the previous user's data. Logging out moves the device to a new anonymous recipient.- When the app knows its user at launch, pass it to
initialize(..., externalId = "user-42"), so the first request already names the user. - The SDK remembers the user, so
initializewithout anexternalIdkeeps it.
Identity verification¶
If the application has identity verification turned on, give Configuration an identityTokenProvider that gets a token for the user from your own backend. The SDK asks it whenever it needs a token:
Kotlin
PlingBuddy.initialize(this, Configuration(
applicationId = "<your application id>",
identityTokenProvider = IdentityTokenProvider { externalId ->
myBackend.plingBuddyToken(externalId)
},
))
identityTokenis a suspend function. Throw when you can't get a token right now, for example while your backend is unreachable: the SDK asks again later.- Return
nullonly when there's no token for this user. After the service refused a request for identity, that stops the sync until the nextlogin,logoutorinitialize. - You can also pass a token to
login("user-42", identityToken = token). - Tokens are kept in memory only, so after a relaunch the SDK asks the provider again.
Recipient data, tags and device properties¶
Kotlin
PlingBuddy.setRecipientData(RecipientData(
email = "[email protected]",
country = "DK", // ISO 3166-1 alpha-2
language = "da", // or a tag such as "pt-BR"
timezone = "Europe/Copenhagen",
metadata = mapOf("plan" to "pro"),
))
PlingBuddy.setTags(mapOf("plan" to "pro", "level" to 3, "beta" to true))
PlingBuddy.removeTags(listOf("beta"))
PlingBuddy.setDeviceProperties(mapOf("model" to Build.MODEL))
- A text field (
email,country,language,timezone) leftnullis unchanged, and an empty string clears it. metadata, tags and device properties are merged into what the service has, so a key that's left out isn't removed; remove a tag withremoveTags.- Data is checked when it's passed in: at most 100 tags, keys of 1 to 64 characters, text values of at most 255 characters, a valid email, a two-letter country. Invalid data is reported to the error listener and not stored.
initializealso takesrecipientDataanddeviceProperties, for data the app has at launch. Of the recipient data, only fields that changed since the last launch are sent.
Notification preference¶
Let users choose between all notifications and only the required ones (see Concepts):
Kotlin
PlingBuddy.setNotificationPreference(NotificationPreference.REQUIRED)
- It belongs to the user, like tags: when they're signed in on several devices, the last choice sent from any of them applies.
RecipientData(notificationPreference = ...)sets it too, also throughinitialize. But a preference passed toinitializeis your choice for whoever is signed in at that launch, so it's sent again for the signed-in user at the nextinitializeafter a login.- Signing in from anonymous doesn't send the device's choice, so a user who already exists keeps theirs. To apply the choice made before signing in, call
setNotificationPreferenceagain afterlogin. - The SDK doesn't read the preference back from the service, so keep the value your settings screen shows yourself.
Country, language and time zone¶
With fetchLocaleAndTimezone = true in the configuration, the SDK reads the recipient's country, language and time zone from the device, instead of the app setting them.
- Country: the region of the device's language and region settings, such as
DK, sent as the device reports it. The service ignores a region that isn't a country, such as419(Latin America). - Language: the first language in the device's settings, or the one the user chose for this app, as a tag such as
da-DK. The service keeps only the language,da, since the country covers the region. - Time zone: the full IANA name, such as
Europe/Copenhagen, never an abbreviation likeCET. - A field the app sets with
setRecipientDatais sent as given; only the others are filled in from the device. Clearing an app-set field gives it back to the device's value. - They're sent with every update, so a new time zone after travelling, or a changed language, reaches the service the next time the app starts.
- Turning it off again stops sending them but doesn't clear what was sent.
- It's off by default. Turned on, the SDK collects this data itself; see Privacy.