Skip to content

Handle on demand notifications with dynamic phone number #16

@alkrauss48

Description

@alkrauss48

See https://laravel.com/docs/11.x/notifications#on-demand-notifications

Under the hood, on demand notifications set the notifiable as AnonymousNotifiable, which have access to $this->routes['your-channel'].

We need to update the Twilio logic when sending a message (see code) to handle on demand notifications. Something potentially like:

private function getRecipientPhoneNumber(mixed $notifiable): string
{
  if ($notifiable instanceof AnonymousNotification && array_key_exists('twilio', $notifiable->routes)) {
    return strval($notifiable->routes['twilio']);
  }

  if ($notifiable->twilioPhoneNumberField !== null) {
    return $notifiable->twilioPhoneNumberField;
  }

  return $notifiable->phone_number;
}

.
.
.

// Send the SMS message
$this->client->messages->create(
    $this->getRecipientPhoneNumber()
    [
        'from' => $this->from_phone_number,
        'body' => $twilioMessage->fullMessage(),
    ]
);

Then update the readme with:

  • On-Demand notification example
  • Set the $twilioPhoneNumberField to a string

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions