quickstart-js icon indicating copy to clipboard operation
quickstart-js copied to clipboard

setbackgroundmessagehandler does not show the message in chrome

Open diegogui1 opened this issue 7 years ago • 6 comments

Hi,

 importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js');
 importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js');


firebase.initializeApp({
  apiKey: "xxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxx",
    databaseURL: "https://xxxxxxxxxxxx",
    projectId: "xxxxxxxxxxxx",
    storageBucket: "",
    messagingSenderId: "xxxxxxxxxxxx",
    appId: "xxxxxxxxxxxx"
})

/*
Retrieve an instance of Firebase Messaging so that it can handle background messages.
*/
const messaging = firebase.messaging()
messaging.setBackgroundMessageHandler(function (payload) {
    console.log('[firebase-messaging-sw.js] Received background message ', payload);
    const notification = JSON.parse(payload.data.notification);
    // Customize notification here
    const notificationTitle = notification.title;
    const notificationOptions = {
        body: notification.body,
        icon: notification.icon
    };
	
	

    return self.registration.showNotification(notificationTitle,
        notificationOptions);
});

Payload:

{
  "data":{
    "notification": {
        "title": "Firebase",
        "body": "test",
        "click_action": "http://xxxxx",
        "icon": "icon.png"
    }},
    "to": "/topics/all"
}

performing debugger, the payload data arrives at the setBackgroundMessageHandler method, when arriving at the self.registration.showNotification does not show the popup message window, and the console does not mark error, chrome desktop Versión 77.0.3865.90 (Build oficial) (64 bits) In mozilla if it shows the popup message

what could be happening?

regards

diegogui1 avatar Sep 20 '19 18:09 diegogui1

I got the same issue in both chrome and firefox

tridungle avatar Oct 11 '19 06:10 tridungle

Ok so assuming this is your SW?

Your initialization for firebase is wrong. it must simple be: firebase.initializeApp({ 'messagingSenderId': '<Your actual sender id>' });

TylerByte666 avatar Oct 21 '19 08:10 TylerByte666

Same issue. in both chrome, firefox and mobile chrome browser. If the tab is not focussed it still shows in console "Handling background message" so in short its going to backgroundeventhandler but not opening any pop up window..

webvoirit avatar Oct 24 '19 19:10 webvoirit

Ok so assuming this is your SW?

Your initialization for firebase is wrong. it must simple be: firebase.initializeApp({ 'messagingSenderId': '<Your actual sender id>' });

I got the same issue in both chrome and firefox

hi r u able to resolve this

webvoirit avatar Oct 24 '19 19:10 webvoirit

Ok so assuming this is your SW? Your initialization for firebase is wrong. it must simple be: firebase.initializeApp({ 'messagingSenderId': '<Your actual sender id>' });

I got the same issue in both chrome and firefox

hi r u able to resolve this

not yet

tridungle avatar Nov 06 '19 08:11 tridungle

notification type messages are handled automatically by the FCM SDK, and are not made available to setBackgroundMessageHandler. This is documented here.

jhuleatt avatar Dec 12 '19 22:12 jhuleatt