Giter VIP home page Giter VIP logo

Comments (11)

martijndwars avatar martijndwars commented on September 12, 2024

Thanks for getting in touch. I don't have the time to look into this right now, but I'll try to look into it this weekend.

from webpush-java.

bschlief avatar bschlief commented on September 12, 2024

Awesome, I'll be around this weekend to answer any questions you might have as well. Thanks again.

from webpush-java.

bschlief avatar bschlief commented on September 12, 2024

Let me know if there's anything I can do to help track this down.

from webpush-java.

martijndwars avatar martijndwars commented on September 12, 2024

Sorry, I didn't get to debugging this. Question: how did you request the subscription on the client side? I'm wondering if the public key that you used to request the subscription matches the key pair you use on the server side. I assume you followed these instructions?

from webpush-java.

bschlief avatar bschlief commented on September 12, 2024

The methodology for creating the VAPID keys is per https://github.com/web-push-libs/web-push/#command-line

Here's a sample freshly created sample of the sort of key we're getting.

16:02:49-bryanschlief~$ npm install web-push -g
/usr/local/bin/web-push -> /usr/local/lib/node_modules/web-push/src/cli.js
/usr/local/lib
└─┬ [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ ├── [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ ├─┬ [email protected]
  │ │ ├── [email protected]
  │ │ └── [email protected]
  │ └── [email protected]
  ├── [email protected]
  └── [email protected]

16:02:57-bryanschlief~$ web-push generate-vapid-keys --json
{"publicKey":"BMzvZylzxhzL7LmFSW7Swj7GGariKK7WAWbk-Q2ESt1apjR2Ek9Rb1tfLSwoli3ww4IUfIlR1-VWATH1tAFJCBw","privateKey":"tZ9kly49ai8KYQQU_11mVSk6VLjzUSHK-xH6vgncKak"}

Here's a sample minimal JS that we're using to create a subscription (using the freshly generated keypair above):

const vapidPublicKey = 'BMzvZylzxhzL7LmFSW7Swj7GGariKK7WAWbk-Q2ESt1apjR2Ek9Rb1tfLSwoli3ww4IUfIlR1-VWATH1tAFJCBw'

function register () {
  if (!navigator.serviceWorker) return
  navigator.serviceWorker.register('push-worker.js')
  .then(registration => {

    return registration.pushManager.getSubscription()
      .then(subscription => {

        if (subscription) {
          return subscription
        }

        return registration.pushManager.subscribe({
            userVisibleOnly: true,
            applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
          }
        )
      })
  }).then(subscription => {
    console.log(subscription)
  })
}

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)
  }
}

---> Registration in JS using new keypair creates this subscription

subscription:
{ endpoint: 'https://fcm.googleapis.com/fcm/send/dLrb97aSFhg:APA91bFBFLNas-R33frcm6CRPOQWzIrOknsmQmvZLNLsoLuzach8lNF3nYPD7SKljznS9Fxc7oB5R9VfsVWeMXjcTy8KrQQ5pAx5XPeMFeMgPjwg8K_S60edodd2D2OBve3Oq5hQvaFx',
 keys:
  { p256dh: 'BORZi3TTCZbVQ7ReaBah6tCNRyr_ooxowjpjXuS0DOxMMtZ8vg__46do3cS8ky-vMjVRTGCIcMau79HNzra1u0A=',
    auth: 'xhvMLfKDkwZ0CR_kb_adKw==' } }

I can send to this subscription successfully using the web-push JS library (https://github.com/web-push-libs/web-push/).

Here's the matching Java test that result in a 400 with Unauthorized Registration

@Test
public void testPushChromeVapid() throws Exception {
    String endpoint = "https://fcm.googleapis.com/fcm/send/dLrb97aSFhg:APA91bFBFLNas-R33frcm6CRPOQWzIrOknsmQmvZLNLsoLuzach8lNF3nYPD7SKljznS9Fxc7oB5R9VfsVWeMXjcTy8KrQQ5pAx5XPeMFeMgPjwg8K_S60edodd2D2OBve3Oq5hQvaFx";

    // Base64 string user public key/auth
    String userPublicKey = "BORZi3TTCZbVQ7ReaBah6tCNRyr_ooxowjpjXuS0DOxMMtZ8vg__46do3cS8ky-vMjVRTGCIcMau79HNzra1u0A=";
    String userAuth = "xhvMLfKDkwZ0CR_kb_adKw==";

    // Base64 string server public/private key
    String vapidPublicKey = "BMzvZylzxhzL7LmFSW7Swj7GGariKK7WAWbk-Q2ESt1apjR2Ek9Rb1tfLSwoli3ww4IUfIlR1-VWATH1tAFJCBw";
    String vapidPrivateKey = "tZ9kly49ai8KYQQU_11mVSk6VLjzUSHK-xH6vgncKak";

    JSONObject jsonObject = new JSONObject();
    jsonObject.append("title", "Hello");
    jsonObject.append("message", "World");

    // Construct notification
    Notification notification = new Notification(endpoint, userPublicKey, userAuth, jsonObject.toString().getBytes());

    // Construct push service
    PushService pushService = new PushService();
    pushService.setSubject("mailto:[email protected]");
    pushService.setPublicKey(Utils.loadPublicKey(vapidPublicKey));
    pushService.setPrivateKey(Utils.loadPrivateKey(vapidPrivateKey));

    // Send notification!
    HttpResponse httpResponse = pushService.send(notification);

    System.out.println(httpResponse.getStatusLine().getStatusCode());
    System.out.println(IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8));
}

Here's a gist of the output: https://gist.github.com/bschlief/7416ee659dd84d06c2d49e6ca98632d0

from webpush-java.

bschlief avatar bschlief commented on September 12, 2024

So, on the recommendation of a brilliant coworker, I recreated the vapid keypair using methodology @martijndwars recommended explicitly :

https://gist.github.com/bschlief/0665d45c0368dcd5d7344221c1f6aa94

And those keys worked like a charm. So i'm closing this.

(here's the test I used that worked: https://gist.github.com/bschlief/3327c209bbe10dfd6cd79060b6ce78b7)

@martijndwars, I don't know if you have a digital tip jar anywhere, but I'd sure love to buy you a beverage/meal. Thanks again.

from webpush-java.

martijndwars avatar martijndwars commented on September 12, 2024

I'm happy you fixed it! It's still strange that keys generated by the JS lib don't work (keys should be independent of the tool that generated them). Hopefully this issue can help others until I've discovered what's causing this issue. Cheers!

from webpush-java.

bschlief avatar bschlief commented on September 12, 2024

I was also super surprised. If you need any more data in the future, I'm happy to help.

from webpush-java.

pradeepprajapat avatar pradeepprajapat commented on September 12, 2024

@bschlief I am implementing push notification using web push lib in javascript and java. When I test application with https://web-push-codelab.appspot.com/ notifications works fine. But same keys as on https://web-push-codelab.appspot.com/ are not working if I am generating notification from java code.

I also tried using VAPID keys generated by openssl as described above, still getting error: 400, unauthorized header .
Could you please help me?

from webpush-java.

martijndwars avatar martijndwars commented on September 12, 2024

Most likely, the keys you're seeing on https://web-push-codelab.appspot.com/ are encoded differently. If you need assistance, please open a new issue so we can keep discussions separated.

from webpush-java.

pradeepprajapat avatar pradeepprajapat commented on September 12, 2024

I generated keys using openssl, and restarted application several times. It solved the issue. Thank you @martijndwars for your help.

from webpush-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.