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.
Here is my service worker:
And here is the code for Push:
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.