Push notifications

A journey's Push step (Retain › Emails) sends a notification to the person's phone or browser. It reaches people who turned notifications on in your app or on your site, signed in. Everyone else skips the step, and their history in the journey says so. Needs @mrrocket/sdk 0.7.0.

Settings › Products › your product › Push notifications shows how many phones and browsers are signed up.

Expo and React Native#

npx expo install expo-notifications expo-constants

After the person signed in (identify), register the device. It asks for the permission, gets the Expo push token and sends it to MRRocket:

import { useMRRocket, usePush } from "@mrrocket/sdk/react-native";
 
const { identify } = useMRRocket();
const push = usePush();
 
identify(user.id, { email: user.email });
const result = await push.register(); // { status: "registered" | "denied" | "not-asked" | "unavailable" | "not-signed-in" | "error" }
  • Call it on every start after sign-in: a device that already allowed notifications registers again without a prompt. push.register({ ask: false }) never shows the prompt.
  • The EAS project id comes from your app config (expo-constants); pass push.register({ projectId }) to set it yourself.
  • reset() on sign-out takes the token back, so the device stops getting that person's notifications. push.unregister() does it by hand.
  • Opening a MRRocket notification counts as Opened on the step. When the step has a link with a scheme (https://…, myapp://billing), the SDK opens it.
  • Push needs a real device: simulators have no push token.

Expo access token. Only when your Expo project has Enhanced security for push notifications on: create a token at expo.dev › Account settings › Access tokens and save it under Settings › Products › Push notifications. It is stored encrypted.

MRRocket sends through Expo's push service and checks Expo's receipts about 15 minutes later. A token Apple or Google no longer accepts (the app was removed) is deleted.

Websites#

Browsers deliver push through a service worker that must come from your own domain.

  1. Download mrrocket-sw.js (Settings › Products › Push notifications, or https://app.mrrocket.dev/mrrocket-sw.js) and serve it from your site's root, so it opens at https://your.site/mrrocket-sw.js. In Next.js put it in public/.
  2. After sign-in, ask from a button (browsers only show the prompt after a click):
<button onclick="mrrocket.push.subscribe().then(function (r) { console.log(r.status) })">Turn on notifications</button>
import { usePush } from "@mrrocket/sdk/next";
 
const push = usePush();
<button onClick={() => push.subscribe()}>Turn on notifications</button>;
  • identify(user.id) must have run first; without a signed-in person the answer is not-signed-in.
  • subscribe({ ask: false }) only sends an existing subscription again, without a prompt; call it on page load after sign-in to keep it fresh.
  • On sign-out call push.unsubscribe() before reset().
  • Your site already has a service worker (a PWA): don't register ours next to it. Add importScripts("/mrrocket-sw.js"); at the top of yours and call push.subscribe({ serviceWorker: false }).
  • Safari on iPhone and iPad gets web push only for sites added to the Home Screen.

The key browsers subscribe with (VAPID) is made by MRRocket for your workspace; there is nothing to set up. Subscriptions a browser drops are deleted on the next send.

Push is a separate consent: the browser or phone asks the person, and MRRocket records it as their push consent. Turning notifications off (or signing out) removes the device.

What MRRocket stores#

Per device: the Expo push token, or the browser's push address and its two keys, linked to the person your app identified. The notification carries the step's title, text and link, and an id so an open can be counted.