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:


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 &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    // Get an array of the values from the submission.
    $values = $webform_submission->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->field_node_owner->entity->field_user_push_token->value;

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

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


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 ?

  • 2
    I might be misunderstanding but it looks like you're just using single quotes when you mean to be using double quotes? 'body' => "Hello $nodeowner this is your body message" – Clive Apr 5 '19 at 20:10
  • Mmmm I just copied the code related to the "Push Notification" from the firebase project page. – Elie Masaad Apr 5 '19 at 21:43
  • @Clive It was because of using the single quote (') so now it is working but the $nodeowner = $values["selected_node_owner"]; is returning the uid instead of the username. Any idea ? – Elie Masaad Apr 5 '19 at 22:54
  • 1
    If it returns UID then you should load the user entity to get the name drupal.stackexchange.com/questions/187142/… – GiorgosK Apr 6 '19 at 5:39

As Clive also mentioned use double quotes instead of single quotes to allow the variables to get replaced inside the string

'body' => "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->getUsername();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.