Push Notifications — Mobile

The "Order #13 is ready for pickup" push needs permission and a token first: Android goes through FCM, iOS through APNs, and Expo Push Service wraps both…

Mrs. Rini moves card #13 to "Ready for pickup", and Wulan's phone chimes right away: "Order #13 is ready for pickup". The app is closed, yet the message still arrives. That's a push notification: a message from a server that shows up on the phone even while the app is closed. It doesn't travel straight there. Say Wulan lives in a gated estate, and Mrs. Rini can't walk in herself to pass on the news. She leaves it with the estate guard, and the guard only delivers if he knows the house number and the resident has said "yes, bring me messages". That guard is Google's and Apple's push service, the house number is the token, and the "yes" is the notification permission.

The path of one push, from the order board to the phone

Step Who What happens
1 App asks the user for notification permission
2 App asks for a token: this phone's address for push
3 App to the Laravel server sends the token, the server stores it per device
4 Server to Expo Push Service "send 'Order #13 is ready for pickup' to this token"
5 Expo to FCM / APNs Android through FCM (Firebase Cloud Messaging), iOS through APNs (Apple Push Notification service)
6 Phone the notification shows; tapped, the app opens Order #13

So before anything can be sent, the app needs permission and a token. Without a token, the server doesn't know which phone to reach.

Permission and token in the app

npx expo install expo-notifications expo-constants   # SDK 57: expo-notifications 57.0.22

Add "expo-notifications" to plugins in app.json. Its plugin adds the aps-environment entitlement on iOS, and the library brings the POST_NOTIFICATIONS permission for Android.

// src/lib/push.ts
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';

export async function registerPush(): Promise<string | null> {
  if (Platform.OS === 'android') {
    // Android 13+: the permission dialog only appears once at least one channel exists
    await Notifications.setNotificationChannelAsync('order-status', {
      name: 'Order status',
      importance: Notifications.AndroidImportance.HIGH,
    });
  }

  let permission = await Notifications.getPermissionsAsync();
  if (!permission.granted && permission.canAskAgain) {
    permission = await Notifications.requestPermissionsAsync();
  }
  if (!permission.granted) return null; // denied: the app still works, just without push

  const projectId = Constants.expoConfig?.extra?.eas?.projectId ?? Constants.easConfig?.projectId;
  const { data } = await Notifications.getExpoPushTokenAsync({ projectId });
  return data; // "ExponentPushToken[...]"
}

The server stores tokens and sends the push

The app sends the token to POST /api/me/push-tokens with {"token":"ExponentPushToken[...]","platform":"android"} and the Bearer login token. The server keeps one row per device: a unique token column, plus user_id and platform. Wulan has a phone and a tablet: two rows. The phone gets used by another account: the same row moves to the new user_id.

When #13 becomes "Ready for pickup", the server posts to https://exp.host/--/api/v2/push/send (up to 100 messages per request):

// on the order board server (Laravel)
$res = Http::acceptJson()->post('https://exp.host/--/api/v2/push/send', [[
    'to' => 'ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]',
    'title' => "Mrs. Rini's Laundry",
    'body' => 'Order #13 is ready for pickup',
    'data' => ['url' => '/order/13'],
    'channelId' => 'order-status',
    'sound' => 'default',
]]);
{ "data": [{ "status": "ok", "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" }] }

Other options for sending a push from the server:

Way When to pick it
Expo Push Service (this lesson) the default: one token format and one request for Android and iOS
Directly to FCM HTTP v1 and APNs from Laravel, with the token from getDevicePushTokenAsync() you need full control and every FCM and APNs feature, or you don't want to depend on Expo's servers. More work: two APIs, FCM wants an OAuth 2.0 access token and APNs wants a JWT from a .p8 key
A third-party push service you want a dashboard, scheduled sends and user segments without building them yourself

According to the Expo docs, expo-notifications doesn't lock you into Expo's service, so the code in the app stays the same; only the token sent to the server differs.

Tapping the notification opens Order #13

data.url holds /order/13, the same address as the src/app/order/[id].tsx screen. In the root layout:

// src/app/_layout.tsx (the push part)
Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowBanner: true, shouldShowList: true, shouldPlaySound: true, shouldSetBadge: false,
  }),
});

function openFromNotification(n: Notifications.Notification) {
  const url = n.request.content.data?.url;
  if (typeof url === 'string' && url.startsWith('/order/')) router.push(url); // only addresses we know
}

// inside RootLayout
useEffect(() => {
  const last = Notifications.getLastNotificationResponse(); // the app was opened FROM a notification
  if (last) openFromNotification(last.notification);
  const sub = Notifications.addNotificationResponseReceivedListener((r) => openFromNotification(r.notification));
  return () => sub.remove();
}, []);

Expo Go, development builds and simulators

Guess first: the push code is correct, Wulan opens the app in Expo Go on an Android phone, and getExpoPushTokenAsync() is called. What happens?

Since SDK 53, push in Expo Go on Android has been removed. getExpoPushTokenAsync() in Expo Go on Android throws: expo-notifications: Android Push notifications (remote notifications) functionality provided by expo-notifications was removed from Expo Go with the release of SDK 53. Use a development build instead of Expo Go. On iOS, Expo Go only warns, but still test with a development build so the permission, the channel and the app name are your own. Local notifications (scheduled by the app itself) still work in Expo Go.

Push can be tested on a real phone, an Android emulator with Google Play services, and the iOS Simulator from Xcode 14 on. The Device.isDevice check from older tutorials is no longer needed.

⚠️ Why doesn't the notification show, or opens the wrong screen?

TL;DR: The "Order #13 is ready for pickup" push needs permission and a token first: Android goes through FCM, iOS through APNs, and Expo Push Service wraps both behind an ExponentPushToken[...] token. On Android create the channel first (order-status), because the Android 13 permission dialog only appears once a channel exists; iOS asks once, or use provisional with no dialog (ios.status PROVISIONAL). The server stores tokens per device, posts to exp.host/--/api/v2/push/send (an "ok" ticket means Expo received it), then reads the receipts about 15 minutes later and deletes DeviceNotRegistered tokens. data: { url: '/order/13' } plus getLastNotificationResponse() and addNotificationResponseReceivedListener open Order #13 when tapped, and setNotificationHandler decides about notifications while the app is open: on SDK 57 it is required for them to show, since SDK 58 they show without it. Android push doesn't work in Expo Go since SDK 53: use a development build.

⚡ Permission and token first

Meaning: Before the server can send a push to Wulan's phone, the app needs notification permission, then a token: that phone's address for push. Without a token, the server doesn't know which phone to reach. Android goes through FCM, iOS through APNs, and Expo Push Service wraps both behind an ExponentPushToken[...] token.

⚡ On Android, the channel first

Write:

await Notifications.setNotificationChannelAsync('order-status', {
  name: 'Order status',
  importance: Notifications.AndroidImportance.HIGH,
});

Meaning: On Android 13 and up, the permission dialog only appears once at least one channel exists. So the channel is created before requestPermissionsAsync(). iOS asks only once, so ask when Wulan has just placed an order.

⚡ A ticket isn't delivery

Guess first: the server sends a push and Expo answers "status": "ok". Has the notification reached the phone?

Not necessarily. An "ok" ticket only means Expo received the message. About 15 minutes later, the server reads its receipt through getReceipts.

Meaning: A receipt with the DeviceNotRegistered error means that phone can't receive push anymore: delete its token row and stop sending.

What you will learn