parse-community / parse-server-push-adapter

A push notification adapter for Parse Server
https://parseplatform.org
MIT License
85 stars 100 forks source link

feat: Add support for web push notifications #239

Closed dplewis closed 1 month ago

dplewis commented 2 months ago

New Pull Request Checklist

Issue Description

I've added Parse.Installation support https://github.com/parse-community/Parse-SDK-JS/pull/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

parse-github-assistant[bot] commented 2 months ago

Thanks for opening this pull request!

dplewis commented 2 months ago

@mtrezza Codecov is broken

parseplatformorg commented 1 month ago

🎉 This change has been released in version 6.1.0