petarov / apns-push-cmd

Send Apple push notifications from your command line
MIT License
4 stars 4 forks source link

Sending push from Windows #8

Open paolopedrielli opened 7 months ago

paolopedrielli commented 7 months ago

I'm trying to use the Windows version to send push notification.

Here is my command prompt: apnscmd_windows_386 -cert-p12 "Test.p12" -token "2af6c2c33ca83e145cba2cd6d296387ba7303ecc1f45b6f6912fb78d766c34bb" -alert-text "Hello" -topic "com.sintesi.ibrama"

And this is the result: apns-push-cmd v1.2 - Apple Push Notification service Command Line Push 2024/04/03 17:13:07 Sending... POST https://api.push.apple.com/3/device/2af6c2c33ca83e145cba2cd6d296387ba7303ecc1f45b6f6912fb78d766c34bb POST /3/device/2af6c2c33ca83e145cba2cd6d296387ba7303ecc1f45b6f6912fb78d766c34bb HTTP/1.1 Host: api.push.apple.com Apns-Expiration: 0 Apns-Push-Type: alert Apns-Topic: com.sintesi.ibrama

2024/04/03 17:13:27 Error in HTTP request: Post "https://api.push.apple.com/3/device/2af6c2c33ca83e145cba2cd6d296387ba7303ecc1f45b6f6912fb78d766c34bb": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

Maybe it's related to HTTP/1.1? I googled this error but it's a generic error, so I really don't know what's wrong.

petarov commented 7 months ago

Make sure it's not a DNS issue. Try resolving the FQDN by running the following on the Windows machine:

nslookup api.push.apple.com

This should return a list of IPv4 and IPv6 addresses.

paolopedrielli commented 7 months ago

Here is the reuslt

Nome: api-vs.push-apple.com.akadns.net Addresses: 2620:149:208:430c::9 2620:149:208:4303::f 2620:149:208:4308::9 2620:149:208:4302::e 2620:149:208:4309::b 2620:149:208:430e::c 2620:149:208:4303::c 2620:149:208:4307::a 17.188.181.9 17.188.182.201 17.188.181.137 17.188.182.204 17.188.180.79 17.188.180.137 17.188.180.77 17.188.182.137 Aliases: api.push.apple.com

So it seems resolved in the right way. I tried to test a push with CURL and the call to the POST reported an HTTP/2

POST /3/device/2af6c2c3xxxxxxxx HTTP/2

so I think the HTTP/1.1 is not correct. Any idea?

petarov commented 7 months ago

I don't think it's the HTTP/1.1. That's just a dump of the request before it gets wired through the HTTP/2 connection. The dump shows approximate request data.

I recon it's a TLS handshake problem that leads to a timeout, but I can't be 100% sure. Try splitting your Test.p12 into a PEM keypair instead and use those files as cert params. See - https://github.com/petarov/apns-push-cmd?tab=readme-ov-file#extract-keys-from-pkcs12

That's my best guess at the moment.

paolopedrielli commented 7 months ago

I extracted the cert and private key from p12 to to pem, so now the command is

apnscmd_windows_386 -cert-file TestCert.pem -cert-key TestPK.pem -token "2af6c2c33ca83e145cba2cd6d296387ba7303ecc1f45b6f6912fb78d766c34bb" -alert-text "Hello" -topic "com.sintesi.ibrama"

Anyway, the behavior it's still the same

2024/04/09 09:42:35 Error in HTTP request: Post "https://api.push.apple.com/3/device/2af6c2c33ca83e145cba2cd6d296387ba7303ecc1f45b6f6912fb78d766c34bb": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

I'll try to test it in different ways, thanks

petarov commented 7 months ago

You could use Curl instead and see if that will work with your certificate files, i.e.

curl -vk --cert-type P12 --cert Test.p12:<Passwort> \
-d '{"aps": {"alert" : "Hello", "sound": "default"}}' \
-H 'apns-topic: <topic>' -H 'apns-push-type: alert' \
https://api.push.apple.com/3/device/<token>

I'd be curious to know if that works.

paolopedrielli commented 7 months ago

I already done this test, and it works! This is the log file

petarov commented 7 months ago

The only way I managed to reproduce this on Windows was by using an expired APNS certificate. If you could provide my with some test certificate I could take another look. Otherwise you could check the latest build, if you care trying again.