pusher / push-notifications-web

Beams Browser notifications
MIT License
39 stars 19 forks source link

Add state to service worker onNotificationReceived #113

Closed daniel-shuy closed 1 year ago

daniel-shuy commented 1 year ago

Closes #64

Adds the following state to the service worker onNotificationReceived handler:

deviceId and userId are stored in IndexedDB. To avoid slowing down the notification, a Promise is used to perform the query asynchronously passed. The listener can then subscribe to the Promise to get the state, for example:

PusherPushNotifications.onNotificationReceived = async ({ pushEvent, payload, statePromise }) => {
  const { userId } = await statePromise;
  pushEvent.waitUntil(
    // custom notification handling logic
  );
};

Because these data are used by the event reporting, I've refactored PusherPushNotifications.reportEvent to reuse the same Promise, to avoid querying IndexedDB twice.