Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onMessage is not called many times #6

Open
gaurav-chandra opened this issue Nov 15, 2017 · 1 comment
Open

onMessage is not called many times #6

gaurav-chandra opened this issue Nov 15, 2017 · 1 comment

Comments

@gaurav-chandra
Copy link

gaurav-chandra commented Nov 15, 2017

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
Copy link

mzvarik commented Jun 14, 2018

See this for more:
firebase/quickstart-js#71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants