Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 606
camargue92 Avatar asked Mar 27 '19 15:03

camargue92


People also ask

How do I send a link in FCM notification?

If you want to send the notification manually from FCM panel then: then make a key like: link and put the value i.e. your fb link. Then send the notification to that particular user or a topic or a segment from FCM panel. You can handle it inside your application like: Extract the data from notification then...

How do you handle notifications when an app is in the background in Firebase web?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.


1 Answers

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!

like image 165
camargue92 Avatar answered Sep 28 '22 02:09

camargue92