Comments (11)
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.
Awesome, I'll be around this weekend to answer any questions you might have as well. Thanks again.
from webpush-java.
Let me know if there's anything I can do to help track this down.
from webpush-java.
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.
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.
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.
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.
I was also super surprised. If you need any more data in the future, I'm happy to help.
from webpush-java.
@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.
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.
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)
- Why don't you set public and private key after creating the pushService? HOT 3
- Which Firewall rules need to be adjusted?
- not able to make push notifications pop up on screen of android phone HOT 3
- Getting 406 status code - Not Acceptable HOT 2
- How to install in Eclipse? HOT 1
- Dependency Dashboard
- Can I use this Library for my
- Can I use it also in react?
- Post request no pass through proxy when PushAsyncService is used.
- Invalid VAPID token: A value in the vapid claims is either missing or incorrectly specified HOT 15
- Thread leak - too many open files HOT 1
- 403 Forbidden issue on Chrome but working on Firefox HOT 4
- PushAsyncService start an HTTP client but provides no way to shut it down, leaving threads running
- Requires parameters that aren't provided by client side
- please release new version with safe dependencies HOT 1
- Webpush not triggering notifcations to safari HOT 14
- Error 403 for chorme
- Sending web push to APNs (https://web.push.apple.com) responds with 403 Forbidden HOT 5
- 403 Forbidden for Chrome HOT 1
- FCM API will be removed in June 2024 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpush-java.