web-push-libs / webpush-java

Web Push library for Java
MIT License
315 stars 113 forks source link

Sending web push to APNs (https://web.push.apple.com) responds with 403 Forbidden #207

Open rsdickerson opened 6 months ago

rsdickerson commented 6 months ago

Has anyone got web push working with iOS Safari (w/PWA)?

I posted the following on StackOverflow, but got no response.

We can send web push from java server to PWA on Android/Chrome works fine, but sending to PWA on iPhone/Safari fails with 403 Forbidden.

Works fine on Android phone

Request to fcm.googleapis.com

url:https://fcm.googleapis.com/wp/evZRV...IeBQGGaRfGK
Authorization=vapid t=eyJ0eXAiOi...o2jHfWJGw, k=BHBlZKwyYa...SclQckMDxE
Content-Encoding=aes128gcm
TTL=2419200
Crypto-Key=p256ecds...lQckMDxE=
Content-Type=application/octet-stream
method:POST
protocol version:HTTP/1.1
entity:[Content-Length: 219,Chunked: false]

Response

statusline:HTTP/1.1 201 Created
Location=https://fcm.googleapis.com/0:1705097911549557%0f493ae6f9fd7ecd
X-Content-Type-Options=nosniff
X-Frame-Options=SAMEORIGIN
X-Xss-Protection=0
Date=Fri, 12 Jan 2024 22:18:31 GMT
Content-Length=0
Content-Type=text/html; charset=UTF-8
Alt-Svc=h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
protocol version:HTTP/1.1
entity:[Content-Type: text/html; charset=UTF-8,Content-Length: 0,Chunked: false]

Fails on iPhone

Request to web.push.apple.com

url:https://web.push.apple.com/QPU8aHza...q44-RonI
Authorization=vapid t=eyJ0eXAiO...DKVX7h5g, k=BHBlZKwy...QckMDxE
Content-Encoding=aes128gcm
TTL=2419200
Crypto-Key=p256ecdsa=BHBlZKwy...clQckMDxE=
Content-Type=application/octet-stream
method:POST
protocol version:HTTP/1.1
entity:[Content-Length: 219,Chunked: false]

Response

statusline:HTTP/1.1 403 Forbidden
content-type=text/plain; charset=UTF-8
apns-id=3597065D-3C81-ED1D-A56C-E5CED97D3BC1
protocol version:HTTP/1.1
entity:org.apache.http.client.entity.DecompressingEntity@6cbc2aee

I'm using the webpush-java library to prepare the web push request. Here's the send code:

JSONObject json = new JSONObject();

json.put("title", "Hello");

json.put("body", "This is a test.");

json.put("sub","mailto:myemail@mycompany.com");

PushService pushService = new PushService(publicKey, privateKey);
Notification notification = new Notification(subscription, json);

HttpPost httppost = pushService.preparePost(notification, Encoding.AES128GCM);

HttpClient httpclient = HttpClients.createDefault();

HttpResponse response = httpclient.execute(httppost);


Any help would be greatly appreciated.

rsdickerson commented 6 months ago

Here's the StackOverflow post for reference. https://stackoverflow.com/questions/77809621/sending-web-push-to-apns-https-web-push-apple-com-responds-with-403-forbidde

charleshope commented 6 months ago

It seems to me that your usage of the library is non-standard. Why bother creating your own Http objects? Maybe try following the simpler usage in the example code?

krissvaa commented 5 months ago

Seems to be related to this: https://github.com/web-push-libs/webpush-java/issues/201#issuecomment-1443258546

don41382 commented 5 months ago

Thanks for the link @krissvaa to the comment. By adding a subject to the PushService it works 🥳 :

PushService(
    "publicKey",
    "privateKey",
    "mailto:example@mail.com",
)
rsdickerson commented 5 months ago

Dang. My bad. I should have updated you on how I got it working. Yes, adding the subject got it going.

The docs on the webpush-java library don't show this way of instantiating the PushService. I did find it clearly shown on webpush.