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

feat: Add support for web push notifications #239

Merged
merged 4 commits into from
May 4, 2024

Conversation

dplewis
Copy link
Member

@dplewis dplewis commented May 3, 2024

New Pull Request Checklist

Issue Description

I've added Parse.Installation support parse-community/Parse-SDK-JS#2119 to the JS SDK. The user can now save a deviceToken and deviceType however their is no way to use Parse.Push with this.

Approach

I've tested this in production and it works great!

Client

const register = await navigator.serviceWorker.register('./worker.js', { scope: '/' });
const subscription = await register.pushManager.subscribe({
  userVisibleOnly: true,
  applicationServerKey: publicVapidKey,
});
const currentInstallation = await Parse.Installation.currentInstallation();
currentInstallation.set('deviceType', 'web');
currentInstallation.set('deviceToken', JSON.stringify(subscription));
await currentInstallation.save();

Service Worker (worker.js)

self.addEventListener('push', (e) => {
  const data = e.data.json();
  self.registration.showNotification(data.alert, {});
});

Server

const PushAdapter = require('@parse/push-adapter').default;
const parseServerOptions = {
  push: {
    adapter: new PushAdapter({
      web: {
        /*  [options](https://github.com/web-push-libs/web-push#api-reference) */
        vapidDetails: {
           subject: 'test@example.com',
           publicKey: 'publicVapidKey',
           privateKey: 'privateKey',
        },
      },
    })
  },
  /* Other Parse Server options */
};

const query = new Parse.Query(Parse.Installation);
query.equalTo('deviceType', 'web');

await Parse.Push.send({
  where: query,
  data: {
    alert: 'Hello World!',
  },
}, { useMasterKey: true });

TODOs before merging

  • Add tests
  • Add entry to changelog
  • Add changes to documentation (guides, repository pages, in-code descriptions)

Copy link

parse-github-assistant bot commented May 3, 2024

Thanks for opening this pull request!

  • ❌ Please link an issue that describes the reason for this pull request, otherwise your pull request will be closed. Make sure to write it as Closes: #123 in the PR description, so I can recognize it.

@dplewis dplewis requested a review from a team May 3, 2024 15:36
@mtrezza mtrezza requested review from a team and removed request for a team May 3, 2024 15:43
spec/WEB.spec.js Outdated Show resolved Hide resolved
Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

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

Looks good, waiting for CI to pass...

@mtrezza mtrezza changed the title feat: Support Web Push Notifications feat: Add support for Web push notifications May 3, 2024
@mtrezza mtrezza changed the title feat: Add support for Web push notifications feat: Add support for web push notifications May 3, 2024
@dplewis
Copy link
Member Author

dplewis commented May 3, 2024

@mtrezza Codecov is broken

@mtrezza mtrezza merged commit 93b6a48 into parse-community:master May 4, 2024
2 of 4 checks passed
parseplatformorg pushed a commit that referenced this pull request May 4, 2024
# [6.1.0](6.0.0...6.1.0) (2024-05-04)

### Features

* Add support for web push notifications ([#239](#239)) ([93b6a48](93b6a48))
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 6.1.0

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.

3 participants