# Omni-channel alerts and push

Canonical page: https://docs.ovok.com/flutter-sdk/omnichannel-alerts

Ovok keeps the project's omni-channel alerts rule and runs it on each alert event Signals sends; the rule names who to tell and over which channel, and Ovok tells them by push and email. `ovok.push` holds a signed-in person's devices and the project's push provider. `ovok.omnichannelAlerts` reads, sets and previews the rule, for project admins. `registerPushDevice` keeps the signed-in person's device registered.

## Register the device after sign-in [#register-the-device-after-sign-in]

The SDK has no push plugin of its own. Pass the token source your app already uses, such as firebase_messaging:

```dart
final messaging = FirebaseMessaging.instance;
await messaging.requestPermission();

final unregister = await registerPushDevice(
  ovok,
  token: messaging.getToken,
  onTokenRefresh: messaging.onTokenRefresh,
  platform: Platform.isIOS ? 'ios' : 'android',
  appId: 'com.example.care',
);
```

- `token` reads the current token; it throws a `StateError` when the source has none yet. `onTokenRefresh` emits each new token, which is registered again.
- Registering the same token again answers the same device. The token is never returned.
- `registerPushDevice` completes with a function. Call it on sign-out, before the session ends: it stops listening and removes the device from Ovok.
- Use `ovok.push.devices.register`, `list` and `unregister` directly when you manage tokens yourself.

## Open the alert from a notification [#open-the-alert-from-a-notification]

Each alert push carries `data` with `type: 'ovok.alert'`, `alertId`, `patientId` and `event`, the Signals alert event it was sent for, such as `alert.triggered` or `alert.refired`. The lock screen shows "New alert" in the person's language, never a name or a value.

```dart
FirebaseMessaging.onMessageOpenedApp.listen((message) {
  if (message.data['type'] == 'ovok.alert') {
    openAlert(message.data['patientId'], message.data['alertId']);
  }
});
```

## Set up push and the rule [#set-up-push-and-the-rule]

```dart
await ovok.push.provider.create({'provider': 'relay', 'url': 'https://push.example.com/ovok'});
await ovok.projects.settings.update('OMNICHANNEL_ALERTS_ENABLED', true);

await ovok.omnichannelAlerts.rules.update({
  'logic': {
    'if': [
      {'==': [{'var': 'event'}, 'alert.triggered']},
      {'notify': {'channels': ['push', 'email'], 'to': ['practitioner', 'caregiver']}},
      {'==': [{'var': 'event'}, 'alert.refired']},
      {'notify': {'channels': ['push', 'email'], 'to': ['relatedPerson', 'sharedWith']}},
      [],
    ],
  },
});

final preview = await ovok.omnichannelAlerts.preview({
  'patientId': patientId,
  'event': 'alert.refired',
});
```

- These calls are for project admins.
- The push provider is `fcm`, `apns`, `expo` or `relay`. A relay's `whsec_` signing secret is answered once, by `create`.
- `logic` is JSON Logic that answers `notify` entries, each naming `channels` and the roles to tell (`to`), or an empty list to tell nobody.
- The logic can read `event`, `alert.id`, `alert.code`, `alert.reason`, `alert.message`, `alert.value` and `patient.id`. `event` is one of `alert.triggered`, `alert.empty`, `alert.note`, `alert.inactive`, `alert.refired`, `alert.resolved` and `alert.acknowledged`.
- Channels are `push` and `email`; roles are `practitioner`, `caregiver`, `relatedPerson` and `sharedWith`. An unknown label (`UNKNOWN_NOTIFY_LABELS`) or a rule that cannot run on every event (`INVALID_NOTIFICATIONS`) throws an `OvokValidationException`.
- `preview` answers the `notify` entries for that event, each with the people its roles resolve to for that patient. `event` is `alert.triggered` when omitted. Add `notifications` to try a rule before you save it. Nothing is sent.

## See also [#see-also]

- [Care platform](/flutter-sdk/care-platform)
- [`registerPushDevice` reference](/flutter-sdk/reference/ovok_sdk/register-push-device#registerpushdevice)
- [`PushResource` reference](/flutter-sdk/reference/ovok_sdk/push-resource#pushresource)
- [`OmnichannelAlertsResource` reference](/flutter-sdk/reference/ovok_sdk/omnichannel-alerts-resource#omnichannelalertsresource)
