Must I use HTTP/2.0 to send Apple Push Notifications? May I use libcurl?
Asked Answered
D

4

10

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!

Deafen answered 24/12, 2015 at 0:27 Comment(5)
curl the command line is good for this scenario with HTTP/2. Just use the --http2 option and and https:// url . So, there must be a way in which you can make libcurl behave also....Altazimuth
HTTP/2 done over a normal HTTP:// URL where we don't know which version it speaks is typically done with Upgrade: HTTPS:// is done differently. libcurl will eventually get prior-knowledge HTTP2 support too and you're welcome to help us implement it.Lenhard
@DanielStenberg I would actually be interested in helping. That sounds fun. How could I get involved?Deafen
you'd be most welcome! Join the curl-library mailing list, state what you want to help out with and we'll guide you from there (and discuss solutions). Also read curl.haxx.se/dev/contribute.html or perhaps this chapter in my coming book: ec.haxx.se/sourcecode-contributing.htmlLenhard
"The Apple Push Notification service (APNs) will no longer support the legacy binary protocol as of November 2020." developer.apple.com/news/?id=11042019aHumour
D
7

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.

Deafen answered 2/3, 2016 at 22:49 Comment(1)
quick check with the sandbox apn server: it delivers push notifications with HTTP/1.1 ... I haven't tested the production one yet.Witham
C
4

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).

Cryptonymous answered 29/7, 2016 at 19:25 Comment(3)
Do you migrate to the new HTTP/2 API? If yes then please let me ask some questions about that. I am also doing the sameAlf
The legacy system doesn't work over HTTPS. It's not HTTP at all. It's a TLS encrypted binary protocol that does not adhere to any standard.Zsolway
The binary API will cease functioning March 31, 2021.Aintab
N
4

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
Nephew answered 21/11, 2018 at 12:51 Comment(0)
A
1

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.

Aintab answered 9/2, 2021 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.