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
Comment #2
c.e.a commentedComment #3
r.nabiullin commentedYou should use a Token or simple str_replace for replacing something in string.
Comment #4
c.e.a commentedWow 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.Comment #5
r.nabiullin commentedI 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]);Comment #6
orabi commentedC.E.A
This is not related to your issue, but may i ask you how you generate the values in
I have a PWA using https://www.drupal.org/project/pwa and im still not sure how to generate those tokens for each user.
Comment #7
c.e.a commentedhttps://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_tokenwhich is just a simple text box field.The token is generated from the application side and not from drupal side.
Hope this will help,
Comment #8
orabi commentedThanks 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
Comment #9
c.e.a commented@Orabi you are welcome, glad I could help.
@nabiyllin referring to the main question of this issue...
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:
You can use:
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,
Comment #10
leopaccanaro commentedHi 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.
Comment #11
leopaccanaro commented