Nickersoft / push-fcm-plugin

Official Firebase Cloud Messaging plugin for Push.js v1.0 :fire:
https://pushjs.org
42 stars 13 forks source link

onMessage is not called many times #6

Open gaurav-chandra opened 6 years ago

gaurav-chandra commented 6 years ago

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.

mzvarik commented 6 years ago

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