In-app messages
Your team writes in-app messages on PurrCRM (Your first in-app message): banners, posts, surveys and carousels. The SDK fetches them, queues them by PurrCRM‘s rules, and sends back what people did. You draw each one, in your app’s look, and tell the SDK what happened.
What the SDK does
Section titled “What the SDK does”- It checks in for messages after a sign-in, when the app comes back to the front, after each batch of events, and when one of your team’s pushes arrives; never sooner than PurrCRM last asked. Your team’s messages appear without a release of your app.
- It keeps the queue’s rules: one banner at a time; one large message (a post, a carousel, a large survey) a session, a session starting again after 30 minutes in the background; a test sent to a test phone first, and never held; a message for some screens held until one of them is on; a message opened by its link first.
- It sends the receipts (seen, clicked, answered, dismissed, completed) in the order the user did them, with the screen and the path through a carousel.
What you draw
Section titled “What you draw”Watch two values:
crm.inApp.next: the post, survey or carousel to show now, one at a time;nilwhile one is showing or none is waiting.crm.inApp.banner: the banner to float over your screens, ornil.
Both stay nil until you call crm.setReady(true) (Sign users in), so no message lands on your sign-in or onboarding screens: what a check-in brought before then waits in the queue, with no receipt sent, and comes as you say ready. setReady(false) takes them away again (they wait on).
crm.inApp.state holds the whole queue (banner, waiting, showing, testPhone), ready or not, if you need it.
@MainActorfinal class MessagePresenter { private var watching: [CrmCancellable] = []
func start() { watching.append(crm.inApp.next.observe { [weak self] m in if let m { self?.present(m) } }) watching.append(crm.inApp.banner.observe { [weak self] banner in self?.drawBanner(banner) // nil takes it away }) }
private func present(_ m: InAppMessage) { switch m.kind { case .post: presentPost(m) case .survey: presentSurvey(crm.inApp.survey(m)) case .carousel: presentCarousel(crm.inApp.carousel(m)) default: crm.inApp.closed(m) // nothing you draw: let the next one come } }}Present a post, survey or carousel the way your app presents a sheet, over whatever screen is on top. Every InAppMessage has an id, a kind, the content of its kind (banner, post, survey or carousel), test, the screens it is for, and large.
Telling the SDK what happened
Section titled “Telling the SDK what happened”crm.inApp takes what the user did; each call sends its receipt:
| Call | When |
|---|---|
shown(m) | It is on screen. A post (or a survey, a carousel) is then showing, and next stays nil until it goes. A banner’s seen receipt is sent once. |
clicked(m, link:) | The user tapped a link or a button in it. |
openLink(m, link) | The same, and the link is followed by the content’s rules; answers true when the link went to a place in your app, so take the message away first. A web page opens outside the app and answers false. |
dismissed(m) | The user left it before its end: a close button, a swipe, Skip. |
completed(m) | The user went through it to its end. |
closed(m) | It is no longer drawn, however it went: the next can show. |
dismissed, completed and closed all free the queue. Call one of them as every message goes, or the next never comes.
Banners
Section titled “Banners”A banner (m.banner, an InAppBanner) is a strip at the top of your screens (style .inline) or a floating card (.floating), with blocks to draw, a link the whole banner goes to, and whether it is dismissible.
func drawBanner(_ b: InAppMessage?) { guard let b, let banner = b.banner else { return removeBannerView() } showBannerView(blocks: banner.blocks, closable: banner.dismissible, onTap: { if !banner.link.isEmpty { crm.inApp.openLink(b, banner.link) } crm.inApp.closed(b) }, onClose: { crm.inApp.dismissed(b) }) crm.inApp.shown(b)}A post (m.post, an InAppPost) is blocks and an optional sender (an Author). Draw it in a sheet; call shown(m) as it shows. A button block’s link goes through openLink(m, link); its close button is dismissed(m); a button that simply finishes it is completed(m).
Surveys
Section titled “Surveys”crm.inApp.survey(m) answers a SurveySession, which keeps the steps, the answers, the way through the survey’s branches, and what is required. Its survey is the Survey: a format (.small fits a sheet, .large takes the screen), an intro and steps. Its state is a CrmValue<SurveyState>:
step: the step shown, an index intosurvey.steps, or-1for a large survey’s intro.answers, by question id;missing, the required questions the user tried to move on without.sending(Next is busy),failed(the submit failed: offer it again),done(submitted: a thank-you step shows, if there is one),over, andcanGoBack.
A step (SurveyStep) is .questions or .thankYou, with questions and blocks. A SurveyQuestion has a type and what it needs: .nps (0 to 10), .stars and .emoji (1 to 5), .numeric (min to max, with minLabel and maxLabel), .singleChoice, .multiChoice and .dropdown (options, and “Other” when other), .shortText (200 characters) and .longText (2000), each with a title, a description and required.
survey.shown() // as it showssurvey.answer(SurveyAnswer(questionId: q.id, number: 9)) // a scoresurvey.answer(SurveyAnswer(questionId: q.id, choices: [option.id])) // a choicesurvey.answer(SurveyAnswer(questionId: q.id, choices: ["other"], other: words))survey.answer(SurveyAnswer(questionId: q.id, text: words)) // words
switch await survey.next() {case .moved, .stayed: break // draw the new state.stepcase .missing(let ids): highlight(ids) // required questions unansweredcase .submitted: break // a thank-you step shows, if anycase .failed: offerToSendAgain()case .closed: takeAway() // finished elsewhere or stopped: say nothing}survey.back() goes back a step; survey.dismiss() is the user leaving before the end (ask “Discard your answers?” first, once they have answered something); survey.finish() puts the thank-you away. survey.fits(question, answer) checks an answer as PurrCRM does. Each step’s answers are saved as the user moves on.
Carousels
Section titled “Carousels”crm.inApp.carousel(m) answers a CarouselSession: screens the user moves through, which can branch on their answers. m.carousel says how to present it (presentation: .sheet, .card or .full; progress: .dots, .bar or .none; whether it can be skipped, skip; whether it is locked, with no way out but its buttons; and its style).
session.screenis the screen to draw: itslayout,image,blocks,background,align, and achoice(options,multiple) if it asks one.session.buttonsare its buttons, with the Next or Continue the SDK adds where the screen needs one.session.stateis aCrmValue<CarouselState>:at(the screen),picked(the options picked on a multiple choice),progress(0 to 1) anddot,canGoBack,busy(a button whose permission the phone is asking for),frozen(the screen’s taps are spent) andover.
session.seen() // once its first screen's pictures are inawait session.tap(button) // a buttonsession.choose(option) // an answer: a single choice moves on at oncesession.swipeForward(); session.back() // swipessession.close() // its close button, or SkipEach button and answer goes where your team drew it; PurrCRM is told each one and the way the user took. A button that asks for a permission goes through your CrmHost.askPermission hook, and the carousel goes on whatever the answer. When state.over turns true, take the carousel away; if it ended on a link, the SDK has followed it (endedOnLink says which). session.done() is for a gesture of your own that puts it away. A link in a screen’s words goes through session.openLink(link).
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 screen as it shows, with the same name every time:
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) 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 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 asks PurrCRM for it and puts it first in the queue, whatever its audience says (Links). await crm.showMessage(id) does the same from your own code.
Content blocks
Section titled “Content blocks”Posts, banners, carousels, surveys, replies and articles carry their words as [ContentBlock]. Draw each block by its type:
type | What to draw |
|---|---|
.paragraph, .heading (level 1 to 3), .eyebrow | spans, sized by textSize, with weight, align and color. |
.bullets, .numbers, .checklist | items, each with its spans. |
.image | imageUrl, with alt for VoiceOver, imageSize, aspect, fit, shape; a link to follow when tapped. |
.button | label, style (.primary, .secondary, .text) and link. |
.callout | spans on a tint (tone or color), with an emoji. |
.divider, .spacer (spacerSize), .emoji | A line, a gap, a large emoji. |
A Span is a run of text, bold, italic, underline, strike, highlight, with its own color and a link. A colour is a theme word (accent, ink, muted, surface, wash, good, warn, bad: map them to your app’s colours, light and dark) or a fixed #rrggbb.
drawnBlocks(blocks) leaves out block types this SDK does not know, so a newer PurrCRM never breaks your screen. plainText(blocks) flattens them to text, a line a block, for a preview or VoiceOver. A link in content goes through crm.inApp.openLink(m, link) in a message, or crm.openLink(link) elsewhere: one of PurrCRM‘s places, a place your followable allows, or an https page; anything else is not followed.
Try them
Section titled “Try them”In any message’s editor on PurrCRM, Try it on my test phone sends it to your phone at once (Test mode). The setup check’s Navigation hooks row warns while nobody watches inApp.next, and its Check in now button asks for messages at once.