12

I would like to know if someone successfully managed to send a push notification through the new APNS API (HTTP2)using CURL.

An example request's given on the APNs Provider API page

Here's how the request must be:

HEADERS

\- END_STREAM

\+ END_HEADERS

:method = POST

:scheme = https

:path = /3/device/00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0

host = api.development.push.apple.com

apns-id = eabeae54-14a8-11e5-b60b-1697f925ec7b

apns-expiration = 0

apns-priority = 10

content-length = 33

DATA

\+ END_STREAM

{ "aps" : { "alert" : "Hello" } }

But with the following command I get error "curl: (16) HTTP/2 stream 1 was not closed cleanly: error_code = 8":

curl \

--verbose \

--http2 \

--cert <APPLICATION_CERT_FILE> \

--key <APPLICATION_KEY_FILE> \

--header "Content-Type: application/json" \

--header ":method: POST" \

--header ":path: /3/device/<DEVICE ID>" \

--data '{ "aps" : { "alert" : "Hello" } }' \

https://api.development.push.apple.com

Any tips?

3 Answers 3

17

I have successfully sent push notifications from cURL using the following:

curl -v -d '{"aps":{"alert":"Test Push","sound":"default"}}' \
--cert /path/to/cert/cert.pem:SECURE_PASSWORD \
-H "apns-topic: com.app.identifier" --http2 \
https://api.development.push.apple.com/3/device/DEVICE_ID

This is using curl version 7.48.0, installed by homebrew:

$ curl --version
curl 7.48.0 (x86_64-apple-darwin15.4.0) libcurl/7.48.0 OpenSSL/1.0.2g zlib/1.2.5 nghttp2/1.9.1
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets 

Notice, however, that I get the same error as you do when using our development certificate on the production server: https://api.push.apple.com/

curl: (16) HTTP/2 stream 1 was not closed cleanly: error_code = 8

3
  • 1
    Thank you for pointing out the dev vs. prod URL. That was my exact issue!
    – Gargoyle
    Jan 16, 2017 at 4:13
  • 1
    As of macOS High Sierra, the "curl" command as in-built now supports http2 so you don't need to use home-brew to install an alternate "curl."
    – chadbag
    Nov 27, 2017 at 14:33
  • YES; I too made the mistake with the https URL which needs to be api.sandbox.push.apple.com for development, Sep 20, 2018 at 9:14
12

Certificate based provider authentication

curl -v
     -d '{"aps":{"alert":"hello"}}'
     -H "apns-topic: <your app bundle ID>" 
     --http2 
     --cert cert.pem
     https://api.push.apple.com/3/device/<device token>

Token based provider authentication

curl -v 
     -d '{"aps":{"alert":"hello"}}'
     -H "apns-topic: <your app bundle ID>" 
     -H "authorization: bearer xxxx.yyyy.zzzz" 
     --http2
     https://api.push.apple.com/3/device/<device token>

You will need to generate your JWT token and sign it using ES256. This is out of scope here (it's easy to find many libraries on a quick Google search).

0
  1. remove --header ":method: POST" (--data will make it use POST)

  2. remove -header ":path: /3/device/"

    The :path part is what you want on the right side of the host name in the URL, so specify a URL like perhaps https://api.development.push.apple.com/3/device/<DEVICE ID>

  3. That said, getting a HTTP/2 level stream error like that is very unexpected and would rather indicate a lower level problem somewhere...

6
  • Thanks for the answer. The point is to experiment with command line before implementing that in C (push server). The problem is that the connection to api.development.push.apple.com must be first initialized and kept persistent at workers initialization stage. Request must be be done then with changing paths according to the device to target...
    – Phil-east
    Feb 25, 2016 at 10:39
  • I cannot give the list of all URLs at initialization time. Reason is that device ids are simply not known by advance since it's a server waiting for incoming requests which are supposed to contain these ids. And even the daemon would know them all by advance (but it's not), there's thousand of devices registering/unregistering and each ID will make a path of its own... HTTP2 is designed to keep a connection with the main host and perform queries through this unique connection by using different paths. Keeping a dedicated connection per whole path is past and what HTTP2 want to make obsolete.
    – Phil-east
    Feb 26, 2016 at 1:06
  • Concerning the low level error, I noticed it still occurs even when removing the invalid --header ":path..." . But using --cert and --key options pointing to valid files as follow: curl --verbose --http2 --cert <APPLICATION_CERT_FILE> --key <APPLICATION_KEY_FILE> --header "Content-Type: application/json" --data '{ "aps" : { "alert" : "Hello" } }' api.development.push.apple.com
    – Phil-east
    Feb 26, 2016 at 1:28
  • I can't figure out if the problem is coming from libnghttp2 or not but what is sure is it enters in the following if statement: if(stream->error_code != NGHTTP2_NO_ERROR)...
    – Phil-east
    Feb 26, 2016 at 1:29
  • If I remove --cert ... and --key ... the command finally returns what the server replied: curl --verbose --http2 --header "Content-Type: application/json" --data '{ "aps" : { "alert" : "Hello" } }' api.development.push.apple.com, but of course the request cannot be served as APNS requires a client certificate to work.
    – Phil-east
    Feb 26, 2016 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.