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

applicationServerKey to Uint8Array #558

Open
PRR24 opened this issue Mar 2, 2020 · 6 comments
Open

applicationServerKey to Uint8Array #558

PRR24 opened this issue Mar 2, 2020 · 6 comments

Comments

@PRR24
Copy link

PRR24 commented Mar 2, 2020

According to MDN https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe
applicationServerKey can be either DOMString or ArrayBuffer.
web-push documentation on the other hand asks to convert VAPID public key to Uint8Array using urlBase64ToUint8Array example function.
I can confirm that no conversion is needed for FF 72 and Chrome 80.
If this is dependent on browser provider/version, please kindly update the documentation.

@marco-c
Copy link
Member

marco-c commented Mar 3, 2020

PRs welcome!

@PRR24
Copy link
Author

PRR24 commented Mar 3, 2020

After looking around, I guess this function originates from https://github.com/GoogleChromeLabs/web-push-codelab and everybody has been copying it since Google published the article.
Sadly this repo is inactive and no common information source has any detailed data.

I would recommend to add an asterisk to the document stating that this function may not be necessary (any more).

@marco-c
Copy link
Member

marco-c commented Mar 3, 2020

No, I think at the beginning that was necessary and so both we and that repo implemented the same.

@PRR24
Copy link
Author

PRR24 commented Mar 4, 2020

Found it. Chromium fixed the problem here: chromium/chromium@c17c199

@marco-c
Copy link
Member

marco-c commented Mar 5, 2020

Good find! We'd need to test other Chromium-based browsers to see when they picked up the fix.

@Yagasaki7K
Copy link

That's example doesn't works?

function urlBase64ToUint8Array(base64String) {
  const padding = '='.repeat((4 - base64String.length % 4) % 4);
  const base64 = (base64String + padding)
    .replace(/-/g, '+')
    .replace(/_/g, '/');
 
  const rawData = window.atob(base64);
  const outputArray = new Uint8Array(rawData.length);
 
  for (let i = 0; i < rawData.length; ++i) {
    outputArray[i] = rawData.charCodeAt(i);
  }
  return outputArray;
}
 
const vapidPublicKey = '<Your Public Key from generateVAPIDKeys()>';
const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);
 
registration.pushManager.subscribe({
  userVisibleOnly: true,
  applicationServerKey: convertedVapidKey
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants