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

Uncaught FirebaseError: Messaging: This method is available in a Window context. (messaging/only-available-in-window).

Open anwaarmirza opened this issue 9 years ago • 9 comments

i want to use messaging.onMessage(function(payload) { console.log("Message received. ", payload); // ... });

in service worker unable to use this getthing this error.

Uncaught FirebaseError: Messaging: This method is available in a Window context. (messaging/only-available-in-window).

anwaarmirza avatar May 10 '17 11:05 anwaarmirza

You need to use this function in the client side javascript for your webapp(the one which registers serviceWorker and requests notification permission from the user). This will capture any push messages sent while your webapp is open in browser window and in focus. You should use this to update your webapp UI to respond to the message received. For service worker just initialise firebase messaging. It will handle any messages sent with the "notification:" payload by default.

For e.g below payload is handled by default if you just initialise firebase messaging in your registered serviceWorker

{"to": "dl*******************************************************************************************8********************************************ujj", "notification": {"title": "Portugal vs. Denmark", "body":"5 to 1", "icon":"https://media.where.com/sql.png" } }

sidyyy911 avatar May 12 '17 07:05 sidyyy911

i want notification click event on foreground

anwaarmirza avatar May 13 '17 08:05 anwaarmirza

If you would like to display notifications you can do one of two

  1. Use non persistent notifications e.g. var notification = new Notification("Hi there!");
  2. Get the service worker registration object(only if you are registering a sw other than the one supplied by default) registrationobject.showNotification(notificationTitle, notificationOptions);

sidyyy911 avatar May 13 '17 23:05 sidyyy911

I need foreground click event

anwaarmirza avatar May 14 '17 08:05 anwaarmirza

navigator.serviceWorker.register('sw.js');

function showNotification() { Notification.requestPermission(function(result) { if (result === 'granted') { navigator.serviceWorker.ready.then(function(registration) { registration.showNotification('Vibration Sample', { body: 'Buzz! Buzz!', icon: '../images/touch/chrome-touch-icon-192x192.png', vibrate: [200, 100, 200, 100, 200, 100, 200], tag: 'vibration-sample' }); }); } }); } I am using this function its working fine, just need foreground click event on this! background click event working fine as well

anwaarmirza avatar May 14 '17 08:05 anwaarmirza

You can try something like this

Notification.requestPermission().then(function(permission){ if(permission==="granted") { var notif=new Notification("Hi there!!"); // similiar format as sw.showNotification(). Check //documentation console.log("Hi"); notif.onclick = function(event) { event.preventDefault(); // prevent the browser from focusing the Notification's tab window.open('http://www.mozilla.org', '_blank'); } } });

sidyyy911 avatar May 14 '17 22:05 sidyyy911

thanks for the feedback :) have already implemented this its works fine on desktop but var notif=new Notification("Hi there!!"); is not supported in mob foreground ,

this is only supporting in both mob and desktop

Notification.requestPermission(function(result) { if (result === 'granted') { navigator.serviceWorker.ready.then(function(registration) { registration.showNotification('Vibration Sample', { body: 'Buzz! Buzz!', icon: '../images/touch/chrome-touch-icon-192x192.png', vibrate: [200, 100, 200, 100, 200, 100, 200], tag: 'vibration-sample' }); }); } });

we will have to adjust foreground onclick notification along with this

any more feedback ?

anwaarmirza avatar May 15 '17 06:05 anwaarmirza

Using new Notification() is not supported on mobile, instead you can only create notifications from a service worker.

If you want to display a notification from the web page, you need to do the following:

navigator.serviceWorker.ready.then((reg) => {
  reg.showNotification('Hello');
});

To then handle a notification click, you need to add an event listener to your service worker:

self.addEventListener('notificationclick', function(event) {
  const clickedNotification = event.notification;
  clickedNotification.close();

  // Do something as the result of the notification click
  const promiseChain = doSomething();
  event.waitUntil(promiseChain);
});

Learn more here: https://web-push-book.gauntface.com/chapter-05/03-notification-behaviour/#notification-click-event

gauntface avatar May 30 '17 20:05 gauntface

navigator.serviceWorker.register('sw.js');

function showNotification() { Notification.requestPermission(function(result) { if (result === 'granted') { navigator.serviceWorker.ready.then(function(registration) { registration.showNotification('Vibration Sample', { body: 'Buzz! Buzz!', icon: '../images/touch/chrome-touch-icon-192x192.png', vibrate: [200, 100, 200, 100, 200, 100, 200], tag: 'vibration-sample' }); }); } }); } I am using this function its working fine, just need foreground click event on this! background click event working fine as well

Can you provide your sample code..

vijayvargiyakhushboo avatar Nov 27 '19 05:11 vijayvargiyakhushboo