2

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.

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.

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.

#SEND PUSH NOTIFICATION
my $push_subscriber = <get subscriber details from db>

my $end_point_host = $push_subscriber->{endpoint};  
my $end_point = "https://$end_point_host/v1/projects/<my project 
  id>/messages:send";

my $request = HTTP::Request->new('POST',$end_point);
$request->header('Authorization'=>"Bearer $bearer_token");
$request->header('Content-Type' => 'application/json');

$request->content(JSON::encode_json ({
  message => {
  token => $push_subscriber->{subscription_id},
     notification => {
        title => 'test',
        body => 'test content'          
     },
     webpush => {
        headers => {
           Urgency => 'high'
        },
        notification => {
            body => 'test content',
            requireInteraction => 'true' 
        }
     }
}}));
#send the request           
$ua->request($request));

Here is the client/browser side javascript that is called when a push notification arrives. This is inside service-worker.js

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)
    );
});

The point where the problem presents itself is pointed out in the above javascript. Any help/feedback would be much appreciated. Thanks.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
jeffez
  • 185
  • 1
  • 12
  • Try using this module https://metacpan.org/pod/Firebase – farzane Jun 12 '19 at 13:09
  • Unfortunately this module has not been updated in 5 years and refers to firebase legacy documentation that was depreciated in 2016. It's also unclear how to use this to trigger push notifications. – jeffez Jun 13 '19 at 01:39

1 Answers1

1

I ended up getting this working by just re-writing my client side subscription code. In my case the bell icon subscription on/off button along with all the js code to make it work.

Basically i went from using googles solution to a firebase specific solution with this guide. https://firebase.google.com/docs/cloud-messaging/js/receive

You only need to store the 'token' on your server and the endpoint is always - https://fcm.googleapis.com/v1/projects/YOUR PROJECT ID/messages:send

The firebase guide contains a sample file where you can subscribe/unsubscribe for push notifications. https://github.com/firebase/quickstart-js/blob/4be200b1c55616535159365b74bfd1fc128c1ebf/messaging/index.html

Once i had this working i could then cut it down and re-write it into just a simple notification button.

For some reason the provided firebase-messaging-sw.js from the guide didn't work for me but using service-worker.js shown in my OP did and so i can now receive push notifications along with their title, body and other data.

This here is how i generate the bearer token used in my OP sample perl code to send out a push notification. Google API OAuth2 : “invalid_grant” error when requesting token for Service Account with Perl

That should hopefully cover everything you need to know if you are wanting to do push notifications on a site with a Perl back end. Hopefully this helps someone else wanting to do the same thing.

jeffez
  • 185
  • 1
  • 12