In-app messages and carousels
In-app messages are switched on for the app on PurrCRM (In-app messages, in the Name and features card on Settings › your app › General). Your team writes them; the SDK checks in for them, decides which is due, and hands it to you.
What you draw: each message, over your screens: a banner floating at the top, a post in a sheet or a screen of its own, a survey’s steps, a carousel’s screens. The SDK keeps the queue and tells PurrCRM what the user did, once you tell it. Never move your own layout for a message: it lies over your screens.
When messages come
Section titled “When messages come”The SDK checks in with PurrCRM when a user signs in, when the app comes back to the front, after each batch of events, and when a push from your team arrives, never closer than PurrCRM asks (20 seconds at first). crm.checkNow() asks at once, whatever the last answer said, and answers how many test messages came (null when it could not ask).
It hands you what is due, by the rules the Flutter SDK keeps:
- nothing before
setReady(true), and nothing again aftersetReady(false):nextandbannerstay null, and what is queued comes once you are ready (Sign users in); - one banner at a time, and no banner while a larger message is showing;
- one large message (a post, a carousel, a large survey) a session; a session starts again after 30 minutes in the background;
- on a test phone, test messages first, and never held back;
- a message aimed at some screens waits until one of them is on.
Observe and draw
Section titled “Observe and draw”crm.inApp has two StateFlow<InAppMessage?>:
crm.inApp.banner: a banner to float over the app;crm.inApp.next: the post, carousel or survey to show now, one at a time.
Collect both in each Activity (or once, in a single-Activity app) for as long as it is on screen:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { launch { crm.inApp.banner.collect { banner -> showBanner(banner) } } launch { crm.inApp.next.collect { message -> showMessage(message) } } } }}In Compose, put the drawing at the root of your content, above your screens:
@Composablefun NorthwindRoot(crm: Crm, content: @Composable () -> Unit) { val banner by crm.inApp.banner.collectAsState() val next by crm.inApp.next.collectAsState() Box(Modifier.fillMaxSize()) { content() banner?.let { BannerOverlay(crm, it) } next?.let { m -> when (m.kind) { MessageKind.Post -> PostSheet(crm, m) MessageKind.Survey -> crm.inApp.survey(m.id)?.let { SurveySheet(crm, m, it) } MessageKind.Carousel -> crm.inApp.carousel(m.id)?.let { CarouselView(crm, m, it) } else -> LaunchedEffect(m.id) { crm.inApp.closed(m.id) } // a kind this build cannot draw } } }}Each InAppMessage has its id, its kind (MessageKind.Banner, Post, Carousel, Survey), the content for that kind (banner, post, carousel, survey), test for a test send, large, and drawable (its kind’s content is there). The setup check warns when nothing collects crm.inApp.next.
Say what the user did
Section titled “Say what the user did”Tell the SDK by the message’s id; each receipt is sent once:
| Call | When |
|---|---|
crm.inApp.shown(id) | You drew it (for a carousel, CarouselSession.seen() says it instead). |
crm.inApp.clicked(id, link) | The user tapped a button or a link in it. The SDK records the click and follows the link; it answers true when the link leads into the app, and you close the message. False, and nothing said, for a link that leads nowhere (one your followable does not allow): do not draw such a button. |
crm.inApp.dismissed(id) | The user closed it before its end: its close button, a swipe, a tap outside, Back. |
crm.inApp.completed(id) | The user went through it to its end. |
crm.inApp.closed(id) | It went away, however it went. The next message comes up. |
Call closed last, whatever happened: until then the SDK holds the next message back.
What each looks like
Section titled “What each looks like”A banner (m.banner: style, blocks, dismissible, link) floats over the top of the screen: BannerStyle.Inline is a strip in one colour from the top, Floating a card under the status bar. Leave the rest of the screen touchable. A tap on it with a link is a clicked; its close button (when dismissible) is a dismissed. Hide it while the keyboard is up.
@Composablefun BannerOverlay(crm: Crm, m: InAppMessage) { val banner = m.banner ?: return LaunchedEffect(m.id) { crm.inApp.shown(m.id) } Card( Modifier.statusBarsPadding().padding(12.dp).fillMaxWidth().clickable(enabled = banner.link.isNotEmpty()) { crm.inApp.clicked(m.id, banner.link) crm.inApp.closed(m.id) }, ) { Row(verticalAlignment = Alignment.CenterVertically) { Blocks(banner.blocks, Modifier.weight(1f).padding(16.dp)) if (banner.dismissible) { IconButton(onClick = { crm.inApp.dismissed(m.id) crm.inApp.closed(m.id) }) { Icon(Icons.Default.Close, contentDescription = "Close") } } } }}A post (m.post: blocks, and the sender) opens in a bottom sheet sized to it, or the whole screen when it is long, with its buttons kept at the foot. A button block’s link goes to clicked; closing the sheet is a dismissed.
A survey (m.survey) that is SurveyFormat.Small opens in a sheet that grows or shrinks with each step; a Large one takes the whole screen and starts with an introduction (intro blocks). Walk it with a SurveySession (below).
A carousel (m.carousel) is screens the user moves through, which can branch on their answers. It opens as presentation says: CarouselPresentation.Sheet (the default), Card in the middle of the screen, or Full. Walk it with a CarouselSession (below).
Draw every message’s blocks as in Drawing content blocks. Follow the phone’s text size, and turn animations off when the user asked for less motion.
Surveys
Section titled “Surveys”crm.inApp.survey(id) answers a SurveySession for a survey waiting or showing (null for any other message). Call crm.inApp.shown(id) when you draw it, then:
state(SurveyState): thestepshowing (an index intosession.survey.steps, or -1 for a large survey’s introduction), theanswersso far by question id,missing(required questions left unanswered on the lastnext),sending,failed,done(sent: the thank-you step when the survey has one, or your own thanks),over, andprogress(this step and how many there are, or null);answer(SurveyAnswer(questionId, number = …, choices = …, other = …, text = …))keeps an answer; false, and nothing kept, for one its question cannot take (a choice it does not offer, words past the limit);next()moves on: to the next step on the user’s way (the survey may branch on answers), or at the end, submits;back()goes back a step; false from the first;dismiss()when the user leaves before the end. Ask “Discard your answers?” first whenstate.answered.
Each step has its questions: a SurveyQuestion of type Nps, Numeric, Stars, Emoji, Dropdown, ShortText, LongText, SingleChoice or MultiChoice, with its title, required, options, other, min/max and their labels, and placeholder. Once state.over (and after your thanks), take the survey away and call crm.inApp.closed(id). A survey PurrCRM says is closed ends quietly, as over.
Carousels
Section titled “Carousels”crm.inApp.carousel(id) answers a CarouselSession for a carousel waiting or showing (null for any other message). Call seen() once its first screen is on show (it stands for shown), then, as the user acts:
button(b)for a button tapped,option(o)for a choice’s option (a single choice answers and moves on; a multiple one toggles),swipeForward()for a swipe (false where a swipe does not move on),back()for Back,close()for the close button or Skip, andopenLink(link)for a link in a screen’s words.
state (CarouselState) is where the user is: at and screen (a CarouselScreen: its blocks, layout, image, background, choice, style), picked options, frozen (moving on: taps do nothing more here), busy (the permission button whose question the phone is asking), canGoBack, progress (0 to 1) and dot, and over and completed. Moving is at once; animate it yourself.
session.flow.buttonsOf(state.at) is the buttons to stack on a screen, the main one first: its own, or the SDK’s Next (Done where it ends) and Continue for a multiple choice, which come with an empty label for you to word. session.flow.progress() says whether to show dots, a bar or nothing. session.carousel.style and each screen’s style carry your team’s accent, corners, button shape and text scale.
A button that asks for a permission (CarouselPermission.Notifications or Location) calls your CrmHost.askPermission hook; the carousel goes on whatever the answer. Without the hook, the button just goes on. Once state.over, take the carousel away and call crm.inApp.closed(id). PurrCRM is told each answer, each button, and the path the user took.
Showing a message on some screens
Section titled “Showing a message on some screens”Your team can show a message only on some screens (“Show on screens”). Name each of your screens as it shows, with the same name every time:
override fun onResume() { super.onResume() crm.screen("wallet")}The SDK holds such a message until one of its screens is on. An app that names no screen is never sent one. crm.screen also records a screen_viewed event (Events).
A message opened by its link
Section titled “A message opened by its link”A post, carousel or survey can be opened by a link (<link base>/messages/show/<id>), for example from an email. handleLink (Links) and crm.showMessage(id) ask PurrCRM for it and put it in crm.inApp.next, whatever its audience says. showMessage answers false from a PurrCRM that does not serve it, or for a message not for this user.
Try them
Section titled “Try them”Against your own PurrCRM, use Try it on my test phone in any message’s editor (Your first in-app message). The setup check’s Check in now asks at once, and says what came and whether your app observes crm.inApp.next.