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

家住魔仙堡 提交于 2020-01-13 08:51:06

问题


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!


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/34445709/must-i-use-http-2-0-to-send-apple-push-notifications-may-i-use-libcurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!