title: Push Notifications in Perl
body: <p>I am implementing push notifications for a site that has a Perl back end. Firebase is the push notification service i am using. I have spent a fair bit of time with this and looked at a number of guides and some useful resources on SO. I have come up with a working implementation with just one issue. The problem is when send out a push notification it seems to arrive on the client/browser as an empty message. That is no data containing the 'title' and 'body' is retrievable on the client/browser side when the push notification arrives.</p>
  
  <p>I have tried using both firebases older and newer api and either way it ends up with the same outcome of empty push notifications arriving on the client/browser. I have tested this on chrome,firefox and android and the same thing happens.</p>
  
  <p>Here is the perl code that sends the push notification. I have excluded generating the bearer token to limit how much code there is to read. </p>
  
  <pre><code>#SEND PUSH NOTIFICATION
  my $push_subscriber = &lt;get subscriber details from db&gt;
  
  my $end_point_host = $push_subscriber-&gt;{endpoint};  
  my $end_point = "https://$end_point_host/v1/projects/&lt;my project 
    id&gt;/messages:send";
  
  my $request = HTTP::Request-&gt;new('POST',$end_point);
  $request-&gt;header('Authorization'=&gt;"Bearer $bearer_token");
  $request-&gt;header('Content-Type' =&gt; 'application/json');
  
  $request-&gt;content(JSON::encode_json ({
    message =&gt; {
    token =&gt; $push_subscriber-&gt;{subscription_id},
       notification =&gt; {
          title =&gt; 'test',
          body =&gt; 'test content'          
       },
       webpush =&gt; {
          headers =&gt; {
             Urgency =&gt; 'high'
          },
          notification =&gt; {
              body =&gt; 'test content',
              requireInteraction =&gt; 'true' 
          }
       }
  }}));
  #send the request           
  $ua-&gt;request($request));
  </code></pre>
  
  <p>Here is the client/browser side javascript that is called when a push notification arrives. This is inside service-worker.js</p>
  
  <pre><code>self.addEventListener('push', function(e) {
      var body;
  
      if (e.data) {//THE PROBLEM IS HERE. No 'data' object exists
          body = e.data.text();
      } else {
          body = "Empty Message";
      }
  
      var options = {
          body: body
      };
      e.waitUntil(
          self.registration.showNotification('My Notification', options)
      );
  });
  </code></pre>
  
  <p>The point where the problem presents itself is pointed out in the above javascript. Any help/feedback would be much appreciated. Thanks.</p>