Expo

updated 2 months ago

In this article:

Engage's Expo integration makes it possible to send push notifications through Expo.

How it works

Engage connects to your Expo account to send Push Notifications created through Campaigns and Automations to your customers. For this to work, the following requirements must have been met.

  1. Your application is built with Expo and uses the Expo notifications service.

  2. You have connected your Expo account with Engage. You can do this via your Engage dashboard → Settings → Integrations → Expo.

  3. You are tracking your users’ expo push token and device platform and sending it to Engage. The device token is how Engage is able to deliver the Push Notification to the targeted recipient. More on this later.

It is important you are tracking expo's push token and not the native device token. There is a difference between both. Expo push tokens generally look something like this: ExponentPushToken[**********************]. If you are tracking the native device token, you should take a look at sending push notifications via FCM instead.

Integration Steps

  • Log in to your Engage account and visit the Settings page.

  • Click on the Integrations tab.

  • Click on the Connect button for Expo. You will be directed to add an optional access token.

  • If you have created an access token as an additional security layer for programmatic access to your Expo account, get the access token and paste it. If not, leave the input field blank.

  • Submit.

How to track device token and platform

As already mentioned, to be able to send Push Notifications, you need to track the customer's device token (Expo push token) and platform. Engage allows you to track up to 5 device tokens per customer.

Here is how to get the Expo push token from the device:

import * as Notifications from 'expo-notifications';
...
const token = (await Notifications.getExpoPushTokenAsync()).data;

If you are using Engage JavaScript SDK (recommended), you can send the token to Engage like this:

import * as Notifications from 'expo-notifications';
import Engage from '@engage_so/js';

Engage.init('your_api_key');
...
const token = (await Notifications.getExpoPushTokenAsync()).data;
try {
  await Engage.identify({
    id: 'unique_user_id',
    device_token: token,
    device_platform: Platform.OS
  })
} catch (e) {}

If you are not using the JavaScript SDK, you can make a direct request to the API like this:

import * as Notifications from 'expo-notifications';
// Polyfill for base64 encode/decode
import {decode, encode} from 'base-64' // npm i base-64
if (!global.btoa) {
  global.btoa = encode;
}
if (!global.atob) {
  global.atob = decode;
}

...
const token = (await Notifications.getExpoPushTokenAsync()).data;
try {
  await fetch('https://api.engage.so/v1/users/unique_user_id', {
    method: 'PUT',
    headers: {
      Authorization: `Basic ${btoa('your_api_key:')}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      device_token: token,
      device_platform: Platform.OS
    }),
  });
} catch (e) {}

Was this article helpful?