node.jsfirebasepush-notificationfirebase-cloud-messaging

How to get `fcm_options.link` functioning in firebase web notifications


I'm trying to get my FCM web notifications to contain a clickable link to my site, using the firebase admin SDK (version 7.0.0) for node.js. As far as I can tell I'm following the documentation to a T, but I'm unable to get the link working. To clarify, my notifications are working fine otherwise, it's just the link that I haven't got to work.

The documentation states:

For notification messages sent from the app server, the FCM JavaScript API supports the fcm_options.link key. Typically this is set to a page in your web app

I've included webpush.fcm_options.link inside my notification message. I've made sure to include an explicit notification payload in my message, as the documentation states that data messages don't support fcm_options.link.

Here's the structure of my message currently:

{
    notification: {
        title: 'Title',
        body: 'Body',
    },
    data: {
       // my data here
    },
    webpush: {
        notification: {
            requireInteraction: true,
            icon: '/icons/notification.png'
        },
        fcm_options: {
            link: 'https://example.com/'
        }
    },
    // android: {},
    // apns: {},
    topic: 'sometopic'
};

Here's the function I'm using to send the message:

const admin = require('firebase-admin')

const sendMessage = message => {
    admin
        .messaging()
        .send(message)
        .then(response => {
            console.log(response)
        })
        .catch(error => {
            console.log(error)
        });
};

The link property should be working according to the documentation: my url includes https and my notification is being sent from the app server, and includes an explicit notification payload. At the moment, clicking on the notification just makes it disappear, with nothing else happening.


Solution

  • UPDATE: I worked out what the issue was - my service worker was using the importScripts function, but I was using an out-of-date version of the firebase script that didn't support fcm_options.link. I changed it to my current version of firebase (5.8.5) and it works. All sorted!