Preferences
Your team gives the app topics on Settings › your app › Messaging rules › Topics, such as “Offers” and “New arrivals”, each going by some of email, push, in the app and chat. A person hears about a topic by a channel only when they are subscribed to it: their own choice, or the topic’s default when they never chose. Consent and topics explains the whole model.
PurrCRM‘s emails link to a preference page on the web. In your app, give people a screen of your own.
What you draw: the preferences screen: a switch for each topic’s channels, an “Unsubscribe from all offers and news” row, and the offers-by-push switch. Preferences are behind no part: anyone signed in may have them. A link to /messages/preferences reaches your navigate hook as CrmPlace.Preferences.
The model
Section titled “The model”CrmPreferences has topics and allOff (every offer and piece of news turned off). Each CrmPreferenceTopic has its id, name and description (in the person’s language, where your team translated them) and its channels: a CrmPreferenceState for each channel the topic goes by, with its channel (CrmPreferenceChannel.Email, Push, InApp, Chat) and whether the person is subscribed.
With the SDK’s state
Section titled “With the SDK’s state”crm.preferencesState keeps a preferences screen’s state, and shows each change at once:
load()reads them (again);state(PreferencesState) has thepreferencesas shown,loading, andmessagewhen they could not be read;choose(topicId, channel, on)turns one channel of one topic on or off;turnAllOff()turns every offer and piece of news off;- both take a
wordingof their own, which otherwise isCrmConfig.preferencesWording(below); savingcounts the changes on their way,keptsays every change is kept, andsaveMessagesays why one was not (what is shown goes back to what PurrCRM keeps).
Changes are sent in order; a change PurrCRM does not keep goes back, and what it keeps is read again.
@Composablefun PreferencesScreen(crm: Crm) { val prefs = crm.preferencesState val state by prefs.state.collectAsState() val scope = rememberCoroutineScope() LaunchedEffect(Unit) { prefs.load() } val p = state.preferences ?: return LazyColumn { for (topic in p.topics) { item(key = topic.id) { Text(topic.name, style = MaterialTheme.typography.titleMedium) } items(topic.channels, key = { "${topic.id}-${it.channel.word}" }) { c -> ListItem( headlineContent = { Text(channelName(c.channel)) }, trailingContent = { Switch( checked = c.subscribed, onCheckedChange = { on -> scope.launch { prefs.choose(topic.id, c.channel, on) } }, ) }, ) } } item { TextButton(onClick = { scope.launch { prefs.turnAllOff() } }) { Text("Unsubscribe from all offers and news") } } }}A PurrCRM older than preferences answers load() with a message.
With your own state
Section titled “With your own state”Read and change the same choices yourself:
val prefs = crm.preferences() ?: return // null when PurrCRM could not be reachedval kept = crm.setPreferences( listOf( CrmPreferenceChoice( topicId = prefs.topics.first().id, channel = CrmPreferenceChannel.Email, subscribed = false, ), ), wording = "northwind/preferences-2",)Both answer null when PurrCRM could not be reached, or is older than preferences. crm.setPreferences(emptyList(), allOff = true) turns everything off. withChoice(p, topicId, channel, on) and allTurnedOff(p) give you the preferences as they will be, to show a change at once.
The words people agreed to
Section titled “The words people agreed to”Each change is kept with the version of the words it was agreed under (wording), so your team can later show what a person saw when they chose. Unless you say otherwise, the SDK sends crm_sdk/preferences-1, the version of the Flutter SDK’s own preference screen’s words. Your screen has words of its own, so give it a version of your own (at most 100 characters), and change it whenever you change the words:
CrmConfig( apiBase = "https://app.purrcrm.com", appKey = BuildConfig.CRM_APP_KEY, appVersion = BuildConfig.VERSION_NAME, preferencesWording = "northwind/preferences-2",)crm.preferencesState and crm.setPreferences send it with every change. A single change can say another: choose(topicId, channel, on, wording = ...), turnAllOff(wording = ...) and crm.setPreferences(..., wording = ...) each take one.
Offers by push
Section titled “Offers by push”crm.offersByPush() and crm.setOffersByPush(on) are the push channel’s own switch, above every topic: a marketing push goes only to people who agreed (Push).