Skip to content

Initialise the SDK

Make one Crm for the life of the app, in Application.onCreate, and start it. Everything else (your Activities, your Firebase service, your screens) reaches the same instance.

app/src/main/java/com/northwind/app/NorthwindApp.kt
import android.app.Application
import com.crm.sdk.Crm
import com.crm.sdk.CrmAuth
import com.crm.sdk.CrmConfig
import com.crm.sdk.CrmHost
import com.crm.sdk.api.CrmFetchedToken
import com.crm.sdk.api.CrmTokens
class NorthwindApp : Application() {
lateinit var crm: Crm
private set
// See "Sign users in".
val tokens = CrmTokens.fetched {
val token = backend.crmToken() ?: return@fetched null // null: nobody is signed in
CrmFetchedToken(token)
}
override fun onCreate() {
super.onCreate()
crm = Crm(
context = this,
config = CrmConfig(
apiBase = "https://app.purrcrm.com",
appKey = BuildConfig.CRM_APP_KEY, // app_..., from the App key card
appVersion = BuildConfig.VERSION_NAME, // your app's own version
debug = BuildConfig.DEBUG, // the setup check, in debug builds only
),
auth = CrmAuth.tokens(tokens),
host = CrmHost(
navigate = { where -> showSupportScreen(where) }, // see "Host hooks" below
),
)
crm.start()
}
}

Name it in your manifest:

app/src/main/AndroidManifest.xml
<application
android:name=".NorthwindApp"
...>

and give the build your app’s key, for example from gradle.properties or your CI’s secrets:

app/build.gradle.kts
android {
buildFeatures { buildConfig = true }
defaultConfig {
buildConfigField("String", "CRM_APP_KEY", "\"${providers.gradleProperty("CRM_APP_KEY").get()}\"")
}
}

The key names your app, it is not a secret: it ships inside every build. Your identity secret never does (Sign users in).

crm.start() returns at once and works on the SDK’s own thread. It:

  • follows your app to the background and back, through ActivityLifecycleCallbacks on your Application;
  • reads your app’s public face from PurrCRM: its name, accent, logo, which parts are switched on, its link domain and languages. It keeps the answer on the phone, so the next start has it at once, even offline. crm.publicApp holds it;
  • restores the queue of product events kept on the phone, and records app_opened (and app_updated on the first start of a new version);
  • on the first run after an install, looks for the link that sent the phone to the store (Links).

Calling it again does nothing more: later calls answer the first one’s Job. Make the Crm with a Context of your app; without the Application, it cannot follow your Activities, and the setup check says so.

Every call may be made from any thread. The SDK keeps its state on one thread of its own:

  • the suspend calls (flush(), checkNow(), registerPushToken(), handleLink(), preferences()…) switch to it, so call them from any coroutine, such as lifecycleScope or viewModelScope;
  • the plain calls (session(), setReady(), track(), screen()…) post to it and return at once;
  • the StateFlows (crm.session, crm.ready, crm.publicApp, crm.appFeatures, crm.unread, crm.notices, and each part’s state) may be collected anywhere: with collectAsState() in Compose, or repeatOnLifecycle in a view;
  • your CrmHost hooks are called on the main thread.

A call that reaches PurrCRM and fails throws a CrmFailure: CrmProblem (PurrCRM said no, with its status, code, detail, requestId and retryAfter), CrmAppUnavailable (the app is switched off, see Sign users in), CrmOffline (not reached, or no answer in time) or CrmContractError (an answer the SDK cannot read). Many calls catch it for you and answer null or false instead; each page says which. A GET is asked twice more after a network failure; nothing else is retried. Each attempt has 10 seconds.

CrmConfig describes your app:

FieldWhat it is
apiBaseYour PurrCRM address, https://app.purrcrm.com. Required.
appKeyYour app’s key (app_…, or app_test_… in a staging build). Required.
appVersionYour app’s version, sent with every request, and kept to record app_updated. Required.
productNameWhat your users call your app. Left out, crm.productName is the name set on PurrCRM.
storagePrefixWhat every key the SDK keeps on the phone starts with: crm unless you name one. Pick it once and never change it, or the queued events are lost.
initialFeaturesThe parts (CrmFeature) to have on a phone that has never reached PurrCRM. None unless you name some.
customEventsYour own event names, which track may record: see Events.
followLifecycleWhether the SDK follows the app to the background and back. On. Turned off, call crm.appPaused() and crm.appResumed() yourself.
autoScreenViewsWhether each Activity shown records a screen view, named by its class. Off.
languageTagA function answering the user’s language (BCP 47), so answers and refusals are in it. Left out, the phone’s.
flushEveryMsHow often queued events are sent: 30 000 (30 seconds).
storeWhere the SDK keeps what it keeps: SharedPreferences (PrefsCrmStore) unless you hand another, such as MemoryCrmStore in a test.
nowThe clock, for tests.
debugThe setup check and the request log. Off. Never on in what your users install.
releaseBuildWhether this is a release build. Left out, it is when the app is not debuggable.
pushChannelsThe notification channels you create for PurrCRM‘s pushes, for the setup check to look for. Name your Android channel here when it is not messages. Left empty, the setup check looks for messages.
loggerWhere the SDK’s few log lines go. Left out, Logcat, tag crm.
preferencesWordingThe version of the words on your own preferences screen, kept with each change of a user’s topics (Preferences). Left out, crm_sdk/preferences-1, the Flutter SDK’s own screen’s.

CrmHost is what your app does for the SDK. The SDK draws nothing, so it is your app that navigates, opens links and asks for permissions. Every hook is optional, and each is called on the main thread:

HookWhat it doesWithout it
navigate(destination)Shows the PurrCRM screen a CrmDestination names (the messenger, a conversation, the tickets, the help center, an article…), which you draw. Answers true when it did.Tapped pushes, links to PurrCRM‘s pages and crm.open(...) go nowhere.
followable(link)Your allow-list: where a link in a message (/offers/123) goes in your app, or null for nowhere.Links into your app go nowhere.
openInApp(location)Goes to a place followable allowed.The same.
openExternal(url)Opens a web address. Answers true when it did.An Intent.ACTION_VIEW.
timeZone()The phone’s IANA time zone.TimeZone.getDefault().id.
askPermission(permission)Asks the phone for a permission a carousel’s button asks for (CarouselPermission.Notifications or Location); answers whether it was granted. A suspend function.The button just goes on.
onUnreadChanged(before, after)The unread count changed: refresh a bell of your own.Nothing else is told; crm.unread still moves.
linksThe places in your app a link may go, for your team’s link picker (Links).Your team types paths by hand.
linkOpened(link)A link was followed: its path, and a saved link’s data, campaign and channel (Links).You do not hear of it.
installReferrer()Play’s install referrer, read once on the first run, as start() runs (Links). A suspend function.A link through an install opens the app at its start.

crm.host may be replaced at any time, for example once your Activity has its navigation. CrmHost is a plain class with no copy: make a new one with every hook you use. Give installReferrer to the CrmHost you pass to the constructor, since it is read as the SDK starts: a host set through crm.host after start() is not asked for it, and the setup check’s Deferred links row turns amber.

navigate hands you a CrmDestination: a place (CrmPlace.Messenger, NewConversation, Conversation, Tickets, Ticket, HelpCenter, Collection, Article, Preferences) and, for one conversation, ticket, collection or article, its id (an id or a slug). Show the screen of yours that draws it. With one Activity for your support screens:

NorthwindApp.kt
private fun showSupportScreen(where: CrmDestination): Boolean {
startActivity(
Intent(this, SupportActivity::class.java)
.putExtra("place", where.place.name)
.putExtra("id", where.id)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
return true
}

With a single Activity and Navigation, route on where.place and where.id instead. where.link is the page’s path in PurrCRM‘s link space (/messages/<id>, /help/<slug>…), which is also what PurrCRM writes in a reply, a push or an article. A message shown by its link (CrmPlace.Message) never reaches navigate: the SDK hands it to crm.inApp.next instead.

crm.open(destination) puts one of these pages on screen from your own code, through navigate. crm.mayOpen(destination) says whether it may be opened now: someone is signed in, and its part is switched on.

The messenger, the help center and in-app messages are switched on for the app on PurrCRM (the Name and features card on Settings › your app › General), and the SDK reads them as it starts. crm.appFeatures is the parts the app has on (CrmFeature.Messenger, HelpCenter, InAppMessages), as a StateFlow for your own ways in, such as a tab. crm.features and crm.has(feature) are the parts the user signed in has.

To hold a part back from some users (behind a feature flag of your own), name the parts they may have in CrmSession.features: they get the parts the app has on and you name. Naming a part never switches on one the app has off.

crm.publicApp.value?.brand (CrmPublicBrand) has your app’s name, accent (a hex colour), logoUrl and supportEmail, as set on PurrCRM. crm.languages is the languages your help center is written in, and crm.productName your app’s name. Use them in your screens so the messenger looks like your team set it up.

crm.dispose() (a suspend call) sends what it can of the queue and lets go of everything. An app that keeps one Crm for its whole life never needs it; a test does.

Sign users in.