push-fcm-plugin icon indicating copy to clipboard operation
push-fcm-plugin copied to clipboard

onMessage is not called many times

Open gaurav-chandra opened this issue 8 years ago • 1 comments

Here is my service worker:

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

if (firebase.apps.length > 0)
    firebase.messaging();

self.addEventListener('message', function (event) {
    if (firebase.apps.length === 0) {
        firebase.initializeApp(event.data);
        firebase.messaging();
    }
    //this is when the sw is first installed
    console.dir(event);
    event.ports[0].postMessage(event.data);
});
self.addEventListener('notificationclick', function(event) {
    console.dir(event);
    event.notification.close();
    // Do something as the result of the notification click
});
self.addEventListener('push', function(event) {
    console.dir(event);
});

And here is the code for Push:

Push.config({
                    FCM: {
                        apiKey: "NOT SHOWN HERE",
                        authDomain: ".firebaseapp.com",
                        databaseURL: "NOT SHOWN HERE",
                        projectId: "NOT SHOWN HERE",
                        storageBucket: "",
                        messagingSenderId: "NOT SHOWN HERE",
                        serviceWorkerLocation:"NOT SHOWN HERE",
                        onMessage: function(payload){
                            console.dir(payload);
                            pushNoti.onMessage(payload);
                        }
                    },
                    fallback: function(payload) {
                        Dialog.alert(payload.notification.title,payload.notification.body);
                    }
                });
pushNoti.onMessage = function(){
var options = {
                    body: message,
                    icon: ONLY_DOMAIN + 'assets/images/logo.png',
                    timeout: 0,//don't auto close
                    tag:Math.random(),//this is needed to create a unique notification
                    onClick: function () {
                        window.focus();
                        this.close();
                    }
                };
                if(typeof close !=='undefined' && (close)){
                    options['requireInteraction'] = true;
                }
                if(typeof link !=='undefined'){
                    options['link'] = link;
                }
                    Push.create(title, options);
                }

Token is saved and I am able to successfully send server side push but many times the onMessage is not triggered but the service worker code is triggered successfully.

Can somebody help me as to why push.js is not creating the push notification? I am on Chrome for Mac.

gaurav-chandra avatar Nov 15 '17 11:11 gaurav-chandra

See this for more: https://github.com/firebase/quickstart-js/issues/71

mzvarik avatar Jun 14 '18 19:06 mzvarik