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');
	$messageService->setRecipients($usertoken);
	$messageService->setNotification([
		'title' => 'Hello',
		'body' => 'Hello $nodeowner this is your body message',
		'badge' => 1,
		'icon' => 'optional-icon',
		'sound' => 'optional-sound',
		'click_action' => 'optional-action',
	]);
	$messageService->setData([
		'score' => '3x1',
		'date' => '2019-04-05',
		'optional' => 'Data is used to send silent pushes. Otherwise, optional.',
	]);
	$messageService->setOptions(['priority' => 'normal']);
	$messageService->send();
    }
}

I am using the above custom code in a webform handler to send a push notification to a specific user.

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

Exampe:
I am expecting the body of the notification message to be something like below (Say the selected_node_owner name is "Charbel Azzi":

Hello Charbel Azzi this is your body message

but instead I am getting the below:

Hello $nodeowner this is your body message

Any help please ?

Comments

C.E.A created an issue. See original summary.

c.e.a’s picture

Issue summary: View changes
r.nabiullin’s picture

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

c.e.a’s picture

Wow fast response ever! Thank you...

before closing the issue can you please guide on hoe to change the 'click_action' => 'optional-action',, currently when I click on the push notification, it takes me to the homepage.

I have tried 'click_action' => $node->toUrl() but still redirect me to the homepage.

r.nabiullin’s picture

I don't know where are you using FCM messages, but I dare to suggest than you should use absolute URL's.
'click_action' =>Url::fromRoute('entity.node.canonical', ['node' => $node->id()], ['absolute' => TRUE]);

orabi’s picture

C.E.A
This is not related to your issue, but may i ask you how you generate the values in

 $usertoken = $node->field_node_owner->entity->field_user_push_token->value;

I have a PWA using https://www.drupal.org/project/pwa and im still not sure how to generate those tokens for each user.

c.e.a’s picture

https://www.drupal.org/project/pwa not sure about this module, never used it before!

For the Firebase module, my app developper is generating this token based on my Firebase account which is unique for each mobile device and then I am tranferring this token to the user profile under the field: field_user_push_token which is just a simple text box field.

The token is generated from the application side and not from drupal side.

Hope this will help,

orabi’s picture

Thanks C.E.A

This helped me go in the right direction
Starting from this module : https://www.drupal.org/project/pwa_firebase_notification , I managed to get the user phone tokens sent to Drupal.
Thanks

c.e.a’s picture

Component: Documentation » Code
Category: Feature request » Bug report
Status: Active » Needs work

@Orabi you are welcome, glad I could help.

@nabiyllin referring to the main question of this issue...

// Send the Push Notification.
	// Token is generated by app. You'll have to send the token to Drupal.
	$messageService = \Drupal::service('firebase.message');
	$messageService->setRecipients($usertoken);
	$messageService->setNotification([
		'title' => 'Hello',
		'body' => 'Hello $nodeowner this is your body message',
		'badge' => 1,
		'icon' => 'optional-icon',
		'sound' => 'optional-sound',
		'click_action' => 'optional-action',
	]);

Using the a single quote (') inside the 'title' and the 'body' was preventing the $variables from rendering the values inside the Title and the body of the push notification!

The right method is to use the Double Quote (")

So Instead of:

'title' => 'Hello',
'body' => 'Hello $nodeowner this is your body message',

You can use:

'title' => "Hello",
'body' => "Hello $nodeowner this is your body message",

If you can update the code available of the module page, it will help so many people...

Fell free to close the issue anytime you like.

Thank you,

leopaccanaro’s picture

Hi C.E.A

I am going to make a mention to the module page, but your comment is basic is PHP knowledge. It's not really related to this module or even Drupal.

Best regards,
Leonardo.

leopaccanaro’s picture

Category: Bug report » Support request
Priority: Major » Minor
Status: Needs work » Closed (works as designed)