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

Add state to service worker onNotificationReceived #113

Conversation

daniel-shuy
Copy link
Contributor

@daniel-shuy daniel-shuy commented Oct 5, 2022

Closes #64

Adds the following state to the service worker onNotificationReceived handler:

  • instanceId
  • publishId
  • deviceId
  • userId
  • appInBackground
  • hasDisplayableContent
  • hasData

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.

Copy link
Contributor

@sonologico sonologico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sonologico sonologico merged commit 691ee32 into pusher:master Oct 27, 2022
@daniel-shuy daniel-shuy deleted the service-worker-onNotificationReceived-state branch October 27, 2022 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for querying Beams data inside the Service Worker
3 participants