# Notifications

> Show notifications sent from the desktop app through your app's own handling on Android. It is off until you pass a handler.

The Notifications panel sends a push notification to your app on an iOS Simulator or Android emulator. On Android, it can also deliver it through your app’s notification handling when the FCM broadcast can’t reach the app.

[Using Notifications in the desktop app](/docs/desktop/notifications/)

## Setup

**Accepts** `false | NotificationsPluginConfig` Opt-in

| Option              | Type                                      | Default | Description                                                                                                                                  |
| ------------------- | ----------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `expoNotifications` | `ExpoNotificationsLike`                   | —       | The `expo-notifications` module. The desktop app's Send to device shows a notification through its handler when FCM can't reach the device.  |
| `onRemoteMessage`   | `(message: RemoteMessageLike) => unknown` | —       | Your handler for FCM messages, such as the function you pass to `messaging().onMessage`. It wins over `expoNotifications` when both are set. |

Pass the handler that shows notifications in your app:

* Expo Notifications

  Pass the `expo-notifications` module. The notification is shown with `scheduleNotificationAsync`, using the payload’s `notification` title and body. A payload with only `data` uses `data.title` and `data.message`. The image isn’t shown.

  ```ts
  import { init } from '@xerlock/client';
  import * as Notifications from 'expo-notifications';

  init({
    notifications: { expoNotifications: Notifications },
  });
  ```

* Your own handler

  Pass the handler you use for foreground FCM messages, such as the one you give to `messaging().onMessage`. It receives an FCM-style message whose `from` is `xerlock`. No system notification is shown: the message only reaches your handler, as an FCM message does while your app is in the foreground.

  ```ts
  import { init } from '@xerlock/client';

  import { showNotification } from './show-notification';

  init({
    notifications: {
      onRemoteMessage: (message) => showNotification(message.notification),
    },
  });
  ```

If you pass both, `onRemoteMessage` is used.

### Shapes

| Member                                                                                                                                                                                                     | Description                                                                                        |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| `scheduleNotificationAsync(request: { content: { title?: string; body?: string; data?: Record<string, unknown>; sound?: boolean \| string; }; trigger: null \| { channelId: string }; }): Promise<string>` | Shows a notification through the app's notification handler. `trigger: null` shows it immediately. |

| Member                                                                                                   | Description                                                              |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| `messageId: string`                                                                                      | Always starts with `xerlock-` for a message sent from the desktop app.   |
| `from: string`                                                                                           | The sender id. Always `xerlock` for a message sent from the desktop app. |
| `sentTime: number`                                                                                       | When the message was sent, in milliseconds since the epoch.              |
| `ttl: number`                                                                                            | Seconds the message may live.                                            |
| `notification?: { title?: string; body?: string; android?: { channelId?: string; imageUrl?: string }; }` | The notification part, if the payload had one.                           |
| `data?: Record<string, string>`                                                                          | The custom data, with string values.                                     |

### Turning it off

Leave out `notifications`, or pass `false`. Sending to an iOS Simulator, and over adb on Android, still works without it.

## Limits and Troubleshooting

* **“Add `notifications` to the Xerlock client config to deliver through the app instead.”**: the FCM broadcast failed and no handler is set. Pass `expoNotifications` or `onRemoteMessage`.
* **“The app has no handler for incoming notifications.”**: pass `expoNotifications` or `onRemoteMessage`.
* **“The app’s notification handler threw an error.”**: the toast shows the error your handler threw.
