How to handle Firebase push notification when site is already open?

Viewed 163

I have a PWA that sends a notification to people when they are being called. It only works when the PWA isn't open, or the site isn't in focus. How can I still receive the notification in the app/site even if it's already in focus? I want the PWA to notice that it received a push-notification even if it's already open, perhaps handle things differently in that case.

I have this code for sending the notification:

function sendNotification(){
    const toSend ={
        notification:
        {
            title:"Video Call!",
            body:"Video call!",
            click_action: "https://....netlify.app/?x="+m,
            sound: "default",
            vibrate: [200, 100, 200, 100, 200, 100, 200]
        },
        to:othermailandtoken[1] 
        };
        
        const jsonString = JSON.stringify(toSend);
        
        
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'https://fcm.googleapis.com/fcm/send', true);
        xhr.onload = function(){
        console.log("google response: ",this.responseText);
        }
        
      xhr.setRequestHeader("Content-type", "application/json");
      xhr.setRequestHeader("Authorization","key=....");
    xhr.send(jsonString);
}
0 Answers
Related