Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Send a push to a phone

Use this guide when your app has an Expo push token and you want an event to reach that phone. For an inbox without push, follow Notify principals in their inbox. Push explains addresses, lock-screen text, delivery receipts, and each phone’s badge.

Run the TypeScript blocks in order as one Node 24 script with api and key from the pages above. These calls run on your server. Replace the fake example token in step 3 with the token your app got from Expo’s getExpoPushTokenAsync; keep the secret key on your server. Use the same project’s environment for every call. If your Expo project requires an access token, set it in EventWolf’s config first.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Create a topic with push on

Set defaults.inbox, defaults.push, and required on the topic. Turn on both channels for principals who haven’t chosen a preference, and let them opt out with required: false.

const { topic: pushTopic } = await api(
  'PUT',
  '/v1/events/topics/urgent-requests',
  key,
  {
    name: 'Urgent requests',
    defaults: { inbox: true, push: true },
    required: false,
  },
);
console.log(pushTopic.defaults); // { inbox: true, push: true }

2. Subscribe the topic to events

Notify the principal named by data.assignee.id when a request is escalated. Give the lock screen its own text and the app a URL to open:

const { subscription: pushSubscription } = await api(
  'POST',
  '/v1/events/subscriptions',
  key,
  {
    name: 'Urgent requests to the assignee',
    filter: { types: ['request.escalated'] },
    target: {
      kind: 'notification',
      topic: 'urgent-requests',
      recipients: { fromPaths: ['assignee.id'] },
      template: {
        title: '{{data.summary}}',
        url: '/requests/{{subject}}',
        push: {
          title: 'A request needs you',
          body: 'Open the app to read it.',
        },
      },
    },
  },
);
console.log(pushSubscription.target.kind); // notification

3. Register the phone at sign-in

Register the token for your own recipient ID, u_lee. Keep the answer’s address ID beside that principal’s session; later calls use it, and no answer holds the token:

const expoToken = 'ExponentPushToken[example-not-a-real-token]';
const { pushAddress } = await api(
  'POST',
  '/v1/events/recipients/u_lee/push-addresses',
  key,
  { provider: 'expo', token: expoToken },
);
const phoneAddressId = pushAddress.id;
console.log(pushAddress.provider, pushAddress.status); // expo active

4. Publish an event that matches

Escalate request r_13 to Lee. Accepting the event creates the notification and queues its push; EventWolf sends it in the background:

const phoneEvent = {
  specversion: '1.0',
  id: crypto.randomUUID(),
  source: '/support/backend',
  type: 'request.escalated',
  subject: 'r_13',
  data: {
    summary: 'Request r_13 needs your attention',
    assignee: { id: 'u_lee' },
  },
};
const phonePublished = await api('POST', '/v1/events/events', key, phoneEvent, {
  'content-type': 'application/cloudevents+json',
});
console.log(phonePublished.result); // accepted

5. Read the delivery and its attempt

List pushes for this event, then read the delivery’s attempts. Wait up to 30 seconds for the send to finish:

const { deliveries: phoneDeliveries } = await api(
  'GET',
  `/v1/events/deliveries?kind=push&eventSequence=${phonePublished.sequence}`,
  key,
);
const [phoneDelivery] = phoneDeliveries;
let phoneDetail = await api(
  'GET',
  `/v1/events/deliveries/${phoneDelivery.id}`,
  key,
);
for (let wait = 0; wait < 30 && phoneDetail.attempts.length === 0; wait += 1) {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  phoneDetail = await api(
    'GET',
    `/v1/events/deliveries/${phoneDelivery.id}`,
    key,
  );
}
const phoneAttempt = phoneDetail.attempts.at(-1);
console.log(phoneDetail.delivery.status, phoneAttempt?.outcome);
// pending succeeded
console.log(
  phoneAttempt?.request.target,
  Boolean(phoneAttempt?.providerMessageId),
);
// expo true

An attempt with outcome: 'succeeded' and a providerMessageId means Expo accepted the send. The delivery stays pending while EventWolf waits for a receipt. delivery.nextAttemptAt is the next check, first scheduled for 15 minutes after the send. A successful receipt makes the delivery succeeded and means Apple’s or Google’s push service accepted it. How a push is delivered explains each state.

6. Mark the phone seen when the app opens

Mint a user token for Lee, read their inbox, and show its newest notification in the app. Then mark this phone seen through that notification, using the address ID saved at sign-in:

const { token: phoneUserToken } = await api('POST', '/v1/user-tokens', key, {
  externalId: 'u_lee',
});
const { notifications: phoneInbox } = await api(
  'GET',
  '/v1/events/inbox',
  phoneUserToken,
);
const [phoneNotification] = phoneInbox;
const phoneSeen = await api(
  'POST',
  `/v1/events/push-addresses/${phoneAddressId}/seen`,
  key,
  { through: phoneNotification.id },
);
console.log(phoneSeen.seenThrough === phoneNotification.id, phoneSeen.unseen);
// true 0

Set the app’s local badge to the answer’s unseen count. If the inbox is empty, skip the seen call. A phone app using the principal’s user token reads /v1/events/inbox and can mark only that principal’s own push addresses.

7. Remove the address at sign-out

Remove the address before discarding its saved ID:

await api('DELETE', `/v1/events/push-addresses/${phoneAddressId}`, key);

The call answers 204 with no body. The Push concept explains how an address moves when another principal signs in on the same phone.

Check it worked

Publish another matching event after sign-out. With no address left for Lee, it creates an inbox notification but no push delivery:

const afterSignOut = await api(
  'POST',
  '/v1/events/events',
  key,
  { ...phoneEvent, id: crypto.randomUUID() },
  { 'content-type': 'application/cloudevents+json' },
);
const { deliveries: afterSignOutPushes } = await api(
  'GET',
  `/v1/events/deliveries?kind=push&eventSequence=${afterSignOut.sequence}`,
  key,
);
console.log(afterSignOut.result, afterSignOutPushes.length); // accepted 0

When a push doesn’t arrive

  • Read the delivery and its last attempt as in step 5. Check status, nextAttemptAt, and the attempt’s error.code and error.message. A providerMessageId means Expo accepted the send. push_send_failed means the send was refused or failed, and push_delivery_failed means Expo reported a handoff failure or no receipt arrived in time.
  • If Expo reports DeviceNotRegistered, the attempt’s error is device_not_registered and the address becomes invalid. Have the app get its current Expo token and register it again at sign-in. Later pushes to an invalid address are skipped with skipReason: 'address_invalid'.
  • If there is no push delivery, check the subscription’s match, the registered recipient, the topic’s defaults, and the principal’s preferences. For a topic that isn’t required, a principal’s push: false overrides the topic’s default; without a preference setting, defaults.push: false leaves push off.
  • On Android, set a notification icon in your app’s config for notifications to show the app’s own icon.

For the delivery states, retries, and badge rules, read Push.