Skip to content

Audiences ​

Use an audience send when the product knows who should be told by role or permission, not by address: "warn everyone with the controllership role that a requisition is late". The product sends the audience; the platform resolves the users and sends the notice.

csharp
var result = await mediator.Send(new SendAudienceNotificationCommand
{
    Audiences = [UserAudience.ForRole(controllershipRoleId)],
    Channel = NotificationChannel.Email,
    OperationId = operationId,
    Subject = "Requisition 42 is late",
    TemplateSlug = "sla.reminder",
    TemplateData = new() { ["requisition"] = 42 },
    NotificationType = "sla.reminder",
    EntityKey = "requisition:42"
});

// result.Data: Resolved, Accepted, Failed, NotificationIds

An audience is UserAudience.ForRole(roleId), UserAudience.ForPermission(name) or UserAudience.TenantAdministrators(). A command takes from 1 to 10 of them; the users are merged without duplicates.

What it needs ​

The handler resolves the audiences through IUserAudienceResolver, which the GrydAuth readers register. A host that has Notifications and no GrydAuth still starts; sending an audience there throws an InvalidOperationException ("Audience notifications need an IUserAudienceResolver"), because it is a configuration error, not a business outcome.

How it behaves ​

  • It runs in the organization frame of the session. A session without an organization is refused with NOTIFICATION_AUDIENCE_TENANT_UNPROCESSABLE.
  • Only e-mail is supported. Any other channel is refused with NOTIFICATION_AUDIENCE_CHANNEL_UNPROCESSABLE.
  • One notification per recipient, through the plain send. The e-mail would otherwise carry every address in the same To. Delivery policies, templates, persistence and idempotency apply to each of them. A failed recipient is counted in Failed and the others are still sent.
  • The audience may not exceed GrydNotificationsOptions.MaxAudienceRecipients (1000 by default; valid values are 1 to 2,147,483,646). Above it, nothing is sent and the result is NOTIFICATION_AUDIENCE_LIMIT_UNPROCESSABLE.
  • Each recipient's OperationId is derived from yours and the user id (a version 8 UUID from a SHA-256 hash), so replaying the same OperationId does not duplicate any notification. With DurableDelivery the OperationId is required; without it the send fails with NOTIFICATION_OPERATION_REQUIRED.
  • An exception inside one of the internal sends stops the loop with part of the audience already notified. For that reason, always pass an OperationId: with it a retry is idempotent, and without it everyone who already received the notice receives it again.
  • There are no attachments in this version.

Large audiences ​

With EnableQueue = false the notifications are sent one after another inside the request. For large audiences turn the queue on or use DurableDelivery.

Released under the MIT License.