This question, as you may have inferred from the title, is really two questions in one.
First Question: Must I use HTTP/2.0 to send Apple Push Notifications?
On the APNs Provider API documentation provided by Apple, the opening paragraphs specify:
The provider API is based on the HTTP/2 network protocol.
There are several other references to HTTP/2.0 throughout the documentation. However I don't see (which is not to say it's not there) anything specifying that HTTP/2.0 must be used. Does this mean that I am allowed to use any HTTP version? Or am I in fact constrained to HTTP/2.0?
I am very familiar with HTTP/1.1 but I know almost nothing about HTTP/2.0, thus if I am able to use my old familiar protocol I would prefer that.
Second Question (predicated on first question): May I use libcurl with APNs?
This question is only relevant given an affirmative answer to the first question. If it's not true that I must use HTTP/2.0 with APNs then I already know that I can use libcurl.
I will be sending many APNs from an already busy server and I would prefer to do it natively - therefore I plan to use libcurl if possible. However I understand that libcurl is somewhat limited when it comes to HTTP/2.0.
The main problem is that when libcurl makes an HTTP/2.0 connection, it actually starts with an HTTP/1.1 request that includes an upgrade
header, and then waits for a 101 Switching Protocols
status line. Is this behavior supported with APNs? Or must I try to use something like nghttp2?
I have found that nghttp2 is somewhat complex and very poorly documented at the moment. I'm worried that if I can't use libcurl I might end up having to implement HTTP/2.0 on my own using sockets (which would be THE WORST).
Any help is appreciated for either question! Thank you, everybody!
A sample script using curl to send http2 push message. Given that you have the authentication key from dev site and your version of curl is compiled with http2 support. The p8 file
should be in the same folder of the script say apns.sh
file. Run>bash apns.sh
#!/bin/bash
deviceToken=96951ABACECA47F34C2F93D8E58591054E6F2B42691B4EADA6935C19A107A524
authKey="./AuthKey_SLDFJSDLB.p8"
authKeyId=SLDFJSDLB
teamId=ABCDET
bundleId=com.mycompany.myapp
endpoint=https://api.development.push.apple.com
apns_collapse_id="score_update"
read -r -d '' payload <<-'EOF'
{
"aps": {
"badge": 2,
"category": "mycategory",
"alert": {
"title": "my title",
"subtitle": "my subtitle",
"body": "my body text message 103"
}
},
"custom": {
"mykey": "myvalue"
}
}
EOF
# --------------------------------------------------------------------------
base64() {
openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
}
sign() {
printf "$1"| openssl dgst -binary -sha256 -sign "$authKey" | base64
}
time=$(date +%s)
header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
jwt="$header.$claims.$(sign $header.$claims)"
curl --verbose \
--header "content-type: application/json" \
--header "authorization: bearer $jwt" \
--header "apns-topic: $bundleId" \
--header "apns-collapse-id: $apns_collapse_id"\
--http2 \
--data "$payload" \
$endpoint/3/device/$deviceToken
OK after a good deal of time I finally found the answer. Yes, HTTP/2 is required to use APNS.
It comes down to a single line in the APNS docs that says
APNs requires the use of HPACK (header compression for HTTP/2), which prevents repeated header keys and values.
which would imply that HTTP/2 is a required part of the protocol.
As it stands right now, Apple is still supporting its legacy v2 (binary) API, which works over HTTPS, so HTTP/2 is only required if you want to use the latest API.
The legacy API is documented in an appendix, but honestly, compared to the HTTP/2 API, it is so horrible that I could not recommend using it.
I can say for sure that the legacy API is supported because I have production code that is using it right now (this is also why I can say the API is horrible, and I am busy migrating it to HTTP/2).
2021 Update YES, the HTTP/2 and JSON method is now required, as the binary protocol is being discontinued as of March 31, 2021.
https://developer.apple.com/news/?id=c88acm2b
To give you additional time to prepare, the deadline to upgrade to the APNs provider API has been extended to March 31, 2021. APNs will no longer support the legacy binary protocol after this date.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With