send method
- String recipient,
- DVNotificationMessage message, {
- DVNotificationProviderKind? provider,
- DVNotificationRoutes? routes,
- DVNotificationPreferences? preferences,
- DateTime? now,
Sends message to recipient on the channels it asks for.
This used to take one provider kind and ignore message.channels
entirely, so the spec's own three-channel example delivered to the local
in-app provider and reported success. Each channel now resolves its own
provider and its own address for this recipient, and every channel's
result comes back - including the ones that were suppressed, which is the
difference between a notification that was withheld and one that was lost.
Pass provider to bypass routing entirely and send through one named
provider, which is what a test or a one-off admin push wants.
Throws when every channel failed for a reason the application could fix - nothing registered, no address, a provider that rejected. A recipient who opted out, or whose quiet hours are in force, is not that: the send returns with those recorded.
Implementation
Future<DVNotificationDelivery> send(
String recipient,
DVNotificationMessage message, {
DVNotificationProviderKind? provider,
DVNotificationRoutes? routes,
DVNotificationPreferences? preferences,
DateTime? now,
}) async {
if (provider != null) {
final selected = _providers[provider];
if (selected == null) {
throw StateError('No notification provider registered for $provider.');
}
if (DVPreviewOutbound.isActive) {
await DVPreviewOutbound.captureNotification(recipient, message, provider);
} else {
await selected.send(recipient, message);
}
return DVNotificationDelivery(
recipient: recipient,
attempts: <DVNotificationAttempt>[
for (final channel in message.channels)
DVNotificationAttempt(
channel: channel,
provider: provider,
outcome: DVNotificationOutcome.delivered,
),
],
);
}
final resolvedRoutes =
routes ?? await _routes?.call(recipient) ?? const DVNotificationRoutes();
final resolvedPreferences = preferences ??
await _preferences?.call(recipient) ??
const DVNotificationPreferences();
final instant = now ?? DateTime.now();
final attempts = <DVNotificationAttempt>[];
for (final channel in message.channels) {
attempts.addAll(await _sendOn(
channel: channel,
recipient: recipient,
message: message,
routes: resolvedRoutes,
preferences: resolvedPreferences,
now: instant,
));
}
final delivery =
DVNotificationDelivery(recipient: recipient, attempts: attempts);
if (delivery.isSilent) {
final broken = attempts
.where((DVNotificationAttempt a) =>
a.outcome == DVNotificationOutcome.notConfigured ||
a.outcome == DVNotificationOutcome.noRoute ||
a.outcome == DVNotificationOutcome.failed)
.toList();
if (broken.isNotEmpty) {
throw StateError(
'Notification to $recipient reached nobody: '
'${broken.join('; ')}.',
);
}
}
return delivery;
}