pusher / push-notifications-web

Beams Browser notifications
MIT License
39 stars 19 forks source link

Postmessage #103

Closed arochedy closed 2 years ago

arochedy commented 2 years ago

Hi,

I wan to call postmessage web worker function but it doesn't work.

I tried

PusherPushNotifications.onNotificationReceived = ({ pushEvent, payload, handleNotification }) => {

  console.log('la');

  postMessage("test");

  pushEvent.waitUntil(handleNotification(payload));
};

and

PusherPushNotifications.onNotificationReceived = ({ pushEvent, payload, handleNotification }) => {
  // Your custom notification handling logic here 🛠️
  // This method triggers the default notification handling logic offered by
  // the Beams SDK. This gives you an opportunity to modify the payload.
  // E.g. payload.notification.title = "A client-determined title!"
  // window.localStorage.setItem("test", payload);
  console.log('la');

  self.postMessage("test");

  pushEvent.waitUntil(handleNotification(payload));
};

and

let currentSelf = self;
PusherPushNotifications.onNotificationReceived = ({ pushEvent, payload, handleNotification }) => {
  // Your custom notification handling logic here 🛠️
  // This method triggers the default notification handling logic offered by
  // the Beams SDK. This gives you an opportunity to modify the payload.
  // E.g. payload.notification.title = "A client-determined title!"
  // window.localStorage.setItem("test", payload);
  console.log('la');

 currentSelf.postMessage("test");

  pushEvent.waitUntil(handleNotification(payload));
};

but none is working 😭 How can I interact from the web worker to the main app ?

Thank you

benw-pusher commented 2 years ago

Are you attempting to share data from the web worker to your app? What error are you receiving? Have you tried client.postMessage("test"); ?

arochedy commented 2 years ago

Yes I want to share data from the web worker to my app : do you know how to do it ?

It's looks like client.postMessage("test"); doesn't work I have "client is undefined"

I tried

  _currentSelf.clients.matchAll().then(c => {
    c[0].postMessage("test");
    console.log(c[0]) })

But I don't have client

benw-pusher commented 2 years ago

Are you able to try the below?

self.clients.matchAll().then(all => all.forEach(client => {
            console.log('sending message');
            client.postMessage(payload.data);
        }));

I have not been able to replicate the issue here. Do you have a tab open on your site when receiving the notification?

benw-pusher commented 2 years ago

I'll close this out as it looks to be related to generic web worker implementation and not this specific library. If you have a specific issue with Pusher Beams feel free to open a new issue or get in touch at https://support.pusher.com/hc/en-us/requests/new