lundi 21 septembre 2020

Firebase push notification not working properly on Web

I'm using firebase for sending push notification on the web and I'm using Reactjs. I'm facing lots of issues while sending the notification to the web. I was using the below code for receiving the notification.

    navigator.serviceWorker.addEventListener("message", function (message) {
                const options = {
                    body: "body",
                    icon: "icon",
                    data: {
                    click_action: "click_action"
                    }
                };
                let myNotification = new Notification(title, options);
    });

But when I used the above code, I was receiving multiple notifications for one notification type. the backend was sending only one notification but due to this code, it is showing multiple notifications to the user for one event. then I found the solution to it. following is the solution which I found.

const options = {
                body: "body",
                icon: "icon",
                data: {
                    click_action: "click_action"
                },
                **tag: "notification-1"**
            };
            let myNotification = new Notification(title, options);

By adding "tag" it was showing me one notification but after this, I faced the new issue. that is, now it was not showing notification of different events if one notification is in the notification section of the taskbar and also it replaces the new notification with the old one. and not showing the popup of that notification. To solve this issue I came up with a solution that is below.

messaging.onMessage((payload) => {
            console.log(payload, "notification payload by onMessage");
            const options = {
                    body: "body",
                    icon: "icon",
                    data: {
                    click_action: "click_action"
                    }
                };
                let myNotification = new Notification(title, options);
});

now I'm using the "onMessage" function for receiving notification and showing popup to the user. it is working on my local system. but it is not called on the server. due to this issue, notifications are also not showing to the user.

I followed the following site instruction for the firebase. you can also check this link to better understand my issues

Please, help me in rectifying this issue. I tried a lot to resolve this issue but I'm not able to find any solution for this issue. if anyone can help me and using firebase for push notification on the web then please help me and guide me.




Aucun commentaire:

Enregistrer un commentaire