Programmatically using values from a loaded entity inside a message I am using Firebase module to send push notifications to a specific mobile user when a webform is submitted.

Below is my custom webform handler:

&lt;?php

namespace Drupal\my_custom_module\Plugin\WebformHandler;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;

use Drupal\node\Entity\Node;

use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;


/**
 * Send Push Notification when a webform submission is submitted.
 *
 * @WebformHandler(
 *   id = "Send Push Notification when a webform submission is submitted",
 *   label = @Translation("Send Push Notification when a webform submission is submitted"),
 *   category = @Translation("Push Notification"),
 *   description = @Translation("Send Push Notification when a webform submission is submitted"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */
class PushNotificationWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */

  // Function to be fired while submitting the Webform.
  public function submitForm(array &amp;$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    // Get an array of the values from the submission.
    $values = $webform_submission-&gt;getData();

    // Get (NID) from text element available on the webform.
    $nidvalue = $values["attached_node"];

    // Load the node.
    $node = \Drupal\node\Entity\Node::load($nidvalue);

    // Get (selected_node_owner) value from the User Entity reference element available on the webform.
    $nodeowner = $values["selected_node_owner"];

    // Get (token) value from the loaded node through the 'field_node_owner' Entity field.
    $usertoken = $node-&gt;field_node_owner-&gt;entity-&gt;field_user_push_token-&gt;value;

    // Send the Push Notification.
    // Token is generated by app. You'll have to send the token to Drupal.
    $messageService = \Drupal::service('firebase.message');
    $messageService-&gt;setRecipients($usertoken);
    $messageService-&gt;setNotification([
        'title' =&gt; 'Hello',
        'body' =&gt; 'Hello $nodeowner this is your body message',
        'badge' =&gt; 1,
        'icon' =&gt; 'optional-icon',
        'sound' =&gt; 'optional-sound',
        'click_action' =&gt; 'optional-action',
    ]);
    $messageService-&gt;setData([
        'score' =&gt; '3x1',
        'date' =&gt; '2019-04-05',
        'optional' =&gt; 'Data is used to send silent pushes. Otherwise, optional.',
    ]);
    $messageService-&gt;setOptions(['priority' =&gt; 'normal']);
    $messageService-&gt;send();
    }
}


Everything is working as expected but the 'Variables' are not rendering any values and printed as they are.

Example:

I am expecting the body of the notification message to be something like below (Say the selected_node_owner name is "Elie Masaad"):

Hello Elie Masaad this is your body message


but instead I am getting the below:

Hello $nodeowner this is your body message


Module maintainer gave the below hint:

You should use a Token or simple str_replace for replacing something in string.

Can anyone guide me on how to pragmatically use tokens for my above requirement ?
As Clive also mentioned use double quotes instead of single quotes to allow the variables to get replaced inside the string

'body' =&gt; "Hello $nodeowner this is your body message"


if $values["selected_node_owner"] gives you the UID here is how to get the username

$account = \Drupal\user\Entity\User::load($values["selected_node_owner"]);
$nodeowner = $account-&gt;getUsername();