Skip to content

In-app messages

Your team writes in-app messages in PurrCRM (Your first in-app message): banners, posts, carousels and surveys. The SDK checks in for them, keeps them in order, walks a carousel or a survey as your team drew it, and tells PurrCRM what the user did with each. Your app presents them: it draws each over its pages, with its own components, and tells the SDK when it shows one and how the user left it.

You drawFrom
The banner showing nowcrm.inApp.banner (InAppMessage | null)
The post, carousel or survey to show nowcrm.inApp.next (InAppMessage | null)
A survey’s stepscrm.inApp.survey(m).state
A carousel’s screenscrm.inApp.carousel(m).state

A user who has in-app messages is checked in for as they sign in, as the page comes back to the front, and after each batch of events is sent. A check-in sooner than PurrCRM‘s hint waits for it, and nothing is checked in for while the page is hidden. Then:

  • at most one banner at a time;
  • posts, carousels and surveys one after another, and at most one large one (a post, a carousel or a large survey) a session. A session ends when the page has been hidden for 30 minutes;
  • copies your team sends to a test phone go first, and are never held back;
  • a message aimed at some screens waits until you name one of them with crm.screen(name);
  • a message opened by its link goes to the front.

next and banner stay null until the user is ready (setReady(true)), and next stays null while one is showing. Nothing shows if nothing subscribes to them: the setup check warns when nothing listens to crm.inApp.next.

src/inapp.ts
crm.inApp.banner.subscribe((m) => {
if (m === null) return hideBanner();
showBanner(m.banner!, {
onShow: () => crm.inApp.shown(m),
onClose: () => crm.inApp.dismissed(m),
onTap: () => m.banner!.link !== "" && crm.inApp.openLink(m, m.banner!.link),
});
});
crm.inApp.next.subscribe((m) => {
if (m === null) return;
switch (m.kind) {
case "post":
return showPost(m.post!, {
onShow: () => crm.inApp.shown(m),
onDone: () => crm.inApp.completed(m), // read to its end, or its last button
onClose: () => crm.inApp.dismissed(m),
onLink: (link) => crm.inApp.openLink(m, link),
});
case "carousel":
return showCarousel(crm.inApp.carousel(m));
case "survey":
return showSurvey(crm.inApp.survey(m));
default:
return crm.inApp.closed(m); // a kind this app does not draw
}
});

Tell the SDK what happened, and it tells PurrCRM, each as a receipt:

CallWhen
crm.inApp.shown(m)You put a banner, a post or a survey on the screen. A carousel says it through its own shown().
crm.inApp.dismissed(m)The user closed it before its end: its close button, a swipe, a tap outside, Escape.
crm.inApp.completed(m)The user reached its end: a post read to its button.
crm.inApp.openLink(m, link)The user followed a link in it. It is followed by the content’s rules (Links); one into your app takes the message away first. Answers "inApp", "web", or null for a link that goes nowhere.
crm.inApp.closed(m)It is off the screen, with nothing more to say.

Each of dismissed, completed and closed takes the message away, and the next one waiting comes into next. However the user leaves a message, it counts as dismissed. Ask crm.linkTargetOf(link) before you draw a button: a button whose link goes nowhere should not be drawn.

The content is ContentBlock[] (Drawing rich text), and its look is yours. What PurrCRM‘s own Flutter SDK does is a good model, and what your team expects when they write one:

  • A banner (m.banner: blocks, style, dismissible, link) lies over the top of the page without moving your layout: a strip in one colour (inline) or a floating card (floating). A tap on it follows its link; a close button shows when it is dismissible.
  • A post (m.post: blocks, and the sender it is from) opens as a dialog or a sheet sized to it; a long one takes the whole screen, with its buttons kept at the foot.
  • A survey that is small opens in a dialog or sheet that grows or shrinks with each step; a large one takes the whole screen.
  • A carousel opens as its presentation says: sheet (the default), a card in the middle of the screen, or full screen.

Follow the browser’s prefers-reduced-motion and keep focus inside a message while it shows.

src/survey.ts
function showSurvey(walk: CrmSurvey) {
const { survey } = walk; // format, intro, steps
walk.shown();
const stop = walk.state.subscribe((s) => {
if (s.over && !s.done) return takeAway(); // dismissed, or closed elsewhere
if (s.step < 0) return drawIntro(survey.intro); // a large survey's introduction
const step = survey.steps[s.step]!; // "questions", or "thank_you" once done
drawStep(step, s.answers, s.missing, { sending: s.sending, failed: s.failed, canGoBack: s.canGoBack });
});
onAnswer = (questionId, given) => walk.answer(questionId, given);
nextButton.onclick = () => walk.next(); // Start, Next or Send
backButton.onclick = () => walk.back();
closeButton.onclick = () => walk.dismiss();
onTakenAway = () => { stop(); walk.close(); }; // once it is off the screen, thanks and all
}

A step is questions or a thank_you. Each question has a type (nps, numeric, stars, emoji, dropdown, short_text, long_text, single_choice, multi_choice), a title, whether it is required, and its options, min and max. answer(questionId, given) keeps an answer ({ number }, { choices }, { other } or { text }; an empty one takes it back).

next() moves on when the step’s required questions are answered (state.missing names those that are not), keeps the answers so far, follows your team’s branches, and submits at the end. failed says the submit did not arrive and the user may press Send again; done says it was submitted, with the thanks step showing if there is one. Once it has an answer, leaving should ask “Discard your answers?”. Call close() when you take it off the screen.

src/carousel.ts
function showCarousel(walk: CrmCarousel) {
const { carousel } = walk.message; // presentation, progress, skip, locked, style
let first = true;
const stop = walk.state.subscribe((s) => {
if (s.over) { stop(); return takeAway(); } // ended or dismissed: the SDK has said which
drawScreen(s.screen, {
buttons: s.buttons, // with an added Next, Continue or Done where your team gave none
picked: s.picked,
busy: s.busy, // a permission button waiting on the browser
progress: carousel.progress === "dots" ? { dot: s.dot } : { bar: s.progress },
back: s.canGoBack,
close: !carousel.locked, // "Skip" in its place when carousel.skip
});
if (first) { first = false; walk.shown(); }
});
onButton = (button) => walk.tap(button);
onOption = (option) => walk.pick(option); // one choice moves on; several toggle
onSwipeLeft = () => walk.swipeForward();
onBack = () => walk.back();
onClose = () => walk.dismiss(); // the close button, Skip, a tap outside, Escape
onLink = (link) => walk.openLink(link);
}

Each button and each answer goes where your team drew it, back() returns through the screens seen, and PurrCRM is told each answer, each button and the way the user took. A button the SDK adds has an empty id and label: say Next, Continue or Done in your own words (walk.flow.endsAtNext(s.at) says whether Next ends it). A locked carousel is left only through its own buttons: no close button, no tap outside, no Escape.

A button can ask for a permission (notifications, location) through your askPermission hook (Initialise); the carousel goes on whatever the answer, and without the hook it just goes on.

Each screen has a layout (centred, image_top, image_full, text), an image, a background, blocks, and align and valign; carousel.style carries the accent, corner, buttonShape and textScale. A word your code does not know reads as "unknown": draw your default.

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:

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.

A post, carousel or survey can be opened by a link (<link base>/messages/show/<id>), for example from an email. Such a link opens your native app on a phone; in a browser it has no web page, so it reaches your page only when you hand it to crm.handleLink yourself, which then shows it over the page (Links). crm.showMessage(id) does the same from code: only a live message whose link opens it, to a user in its audience. It answers whether PurrCRM had one to show.

Against your own PurrCRM, make yourself a test phone under Testing › Preview on my phone, then press Try it on my test phone in any message’s editor. crm.inApp.testPhone is true for a test phone, and crm.checkNow() checks in at once, whatever the hint, answering how many tests arrived. With debug on, the setup check’s Check in now does the same and says why nothing shows if nothing does.