# Push notifications (/docs/starter/modules/push)

Choose with `--push expo`, `--push native`, or `--push none` (default), with Starter v0.30.0 or newer. Push notifications go to a signed-in user's phones, so they need an auth module and a mobile app: the [native app](../native-app.mdx) (`--mobile expo`), the [mobile app](../mobile-app.mdx) (`--shell capacitor`), or both, with `--api hono` or `--api fastapi`, which send them.

| Option | Sends through | To |
| --- | --- | --- |
| `expo` | [Expo's push service](https://docs.expo.dev/push-notifications/overview/), with credentials EAS keeps for the app | The native app |
| `native` | FCM for Android and APNs for iOS, straight from the API with its own credentials | The native app and the mobile app |

The CLI refuses the selections that can't work, with the reason:

```text
Push notifications go to the mobile apps, and this project has none: add one with --mobile expo or --shell capacitor, or choose --push none.
Expo's push service reaches only the native app, not the mobile shell: choose --push native, which sends through FCM and APNs to both.
```

## What the apps do [#what-the-apps-do]

- **A switch in the settings:** turning notifications on asks for the phone's permission, then saves the device's push token with the API (`POST /api/push/devices`) for the signed-in user. **Send a test notification** sends one to each of their devices (`POST /api/push/test`, 5 a minute).
- **The token stays current:** the app sends it again at each start, as a push service can change it, and forgets it when notifications are turned off or the user signs out (`POST /api/push/devices/remove`, which needs no session), so a signed-out phone gets no more of the account's notifications.
- **A tapped notification** opens the screen its data names, `url`, such as `/settings`: only the app's own screens, never another site.
- **Tokens that stop working** are forgotten: the API removes those its push service refuses for good, such as an uninstalled app's.

## Send your own [#send-your-own]

From the API's code, such as a background job, with Hono:

```ts title="apps/api/src/jobs/report.ts"
import { push } from "@/server/push";

await push.send(user, {
  title: "Your report is ready",
  body: "Open it in the app.",
  data: { url: "/" },
});
```

With FastAPI:

```python title="apps/api/src/clubedge_api/jobs.py"
from clubedge_api.push import PushMessage
from clubedge_api.push_service import push_notifications

await push_notifications().send(
    user, PushMessage("Your report is ready", "Open it in the app.", {"url": "/"})
)
```

`send` resolves how many devices the push service took the notification for, and `None` (`null` in TypeScript) while no push service is set up. The devices live in the `push_devices` table of `packages/push`, with its own migrations, which `pnpm db:migrate` applies; deleting a user deletes their devices.

## Expo's push service [#expos-push-service]

With `--push expo`, Expo delivers to the native app's Expo push tokens through FCM and APNs. The API needs no credentials of its own.

<Steps>
<Step>
Link the app to an Expo project with `eas init` ([Store releases](../native-app.mdx#store-releases)): Expo push tokens need the project's id.
</Step>
<Step>
Android: create a [Firebase](https://console.firebase.google.com) project, add an Android app with your app's id, save its `google-services.json` in `apps/mobile`, and set `GOOGLE_SERVICES_JSON=./google-services.json` in `apps/mobile/.env.local`. EAS builds take the file from an EAS file variable of that name. Then upload the Firebase project's service account key to EAS: `pnpm dlx eas-cli@latest credentials`, Android, **Google Service Account**, **Push Notifications (FCM V1)**.
</Step>
<Step>
iOS: an interactive `eas build` or `eas credentials` sets up the app's push key with your Apple Developer account.
</Step>
</Steps>

Set `EXPO_ACCESS_TOKEN` in the API's environment only if you turn on enhanced push security in the Expo project's settings.

## FCM and APNs [#fcm-and-apns]

With `--push native`, the API sends straight to Google's and Apple's services. A platform whose credentials are unset gets no notifications, and its devices' tokens wait for them; `pnpm check:env` refuses a service with only some of its credentials.

| Platform | The apps need | The API needs |
| --- | --- | --- |
| Android | Firebase's `google-services.json`: in `apps/mobile` with `GOOGLE_SERVICES_JSON` set (the native app), and in `apps/shell`, which each build copies into `android/app` (the mobile app) | A service account key (Project settings, Service accounts): `FCM_PROJECT_ID`, `FCM_CLIENT_EMAIL`, and `FCM_PRIVATE_KEY`, whose line breaks can be written as `\n` |
| iOS | Push Notifications turned on for the app's identifier in the Apple Developer portal | An APNs authentication key (Keys, with Apple Push Notifications service): `APNS_KEY_ID`, `APNS_TEAM_ID`, `APNS_PRIVATE_KEY` (the .p8 file's content), and `APNS_TOPIC`, the app's id |

Builds from Xcode use Apple's sandbox, which needs `APNS_ENVIRONMENT=development`; TestFlight and the App Store use production, the default. The native app's build adds the push capability itself, and each build of the mobile app adds the push entitlement to `apps/shell/ios` and the app delegate's methods that pass the device's token to Capacitor's plugin.

Use a phone to try notifications: simulators get no push token.

## The packages [#the-packages]

- `packages/push`: the `PushProvider` interface, the devices table and its migrations, and `sendToUser`.
- `packages/push-expo`: Expo's push service, which sends 100 notifications a request.
- `packages/push-native`: FCM's HTTP v1 API and APNs over HTTP/2, with only Node.js's crypto, HTTP/2, and fetch.

FastAPI has the same in `push.py`, `push_expo.py`, and `push_native.py`, whose FCM and APNs need `cryptography` and `h2`: the `push-native` dependency group, which only images with `--push native` install. Projects without the module have none of them, nor the apps' notification packages (CLI 0.46.0 or newer).
