Alex Crist
Alex Crist

Reputation: 1099

How do you identify the Expo project that an Expo Push Notification Token belongs to?

Premise

For better or for worse, I use two Expo accounts for my production and development environments.

Production  Expo Account: prod-proj
Development Expo Account: dev-proj

I use Expo's push notification service to send push notifications to my users. I store each user's Expo Push Notification Token on their user document. i.e.:

User
  id:    1
  name:  Jimothy
  token: ExponentPushToken[di3ja!-lk2^(24af]

Through an unfortunate series of events, most users in my database have a push notification created using the prod-proj Expo project, but a few users have a push notification created using dev-proj.


Problem

When I try to chunk and send push notifications to all my users, I get an error from Expo:

Error: All push notification messages in the same request must be for the same project; separate your push notifications by project.

But my tokens are all mixed up!

How can I separate the Expo Push Notification Tokens by project?

Upvotes: 4

Views: 7786

Answers (2)

Diego Ernst
Diego Ernst

Reputation: 11

I'm facing the same issue right now and probably the best thing we can do is to send the expo push token along with the experience id to the backend. From there, group tokens by experience and send those groups as we'd normally do.

import Constants from "expo-constants";
const experienceId = Constants.manifest.id; // @user/project-slug

Upvotes: 1

Alex Crist
Alex Crist

Reputation: 1099

Figured out the answer to this one!

In the expo-server-sdk Node client for Expo, the details field of the error returned by Expo contains all the information about which tokens belong to which projects.

Example Code

const { Expo } = require('expo-server-sdk');

const expo = new Expo();

const sendNotifications = async () => {

  const notifications = [ ... ];
  const chunks = expo.chunkPushNotifications(notifications);

  for (const chunk of chunks) {

    try {
      await expo.sendPushNotificationsAsync(chunk)
    } 

    catch (error) {
      console.log(JSON.stringify(error));

      // The important part:
      // error.details = { EXPO_PROJECT_NAME: [ ExponentPushTokens ] }
    }
  }
}

Upvotes: 2

Related Questions