iOS Push Notification with HTTP2
Setup iOS APNS with HTTP/2
Overview:
There're one issue while sending iOS notification to multiple devices with old method (send by ftp write stream). Let say you send to 100 devices, ftp write work likes this:
1. connect to APNS Server
2. for each device: send notification by using device_token & payload
3. disconnect
In step 2, if there're 1 invalid token (there're many reason make a token invalid: expired, wrong cert, wrong format...etc) the connection will be block without status from APNS Server, if you want to check the status, you have to wait at least 0.5s-1s and using ftp-read to read the status. It's mean you have to check status after every ftp-write and re-connect again if there're an invalid token.
So, to send to 100 devices, you need 50 seconds, that's too long.
At WWDC 2015, Apple has introduce a new notification api by using HTTP/2
This api allow you to send notification as a post request or curl and get the result immediately, if 1 token is invalid, it's just simple cannot send, there're no connect & disconnect problem.
References:
1. Requirements:
Unfortunately, to using new APNS, you need to update these things:
+ openssl >= v1.0.2
+ curl >= 7.46.0 and build with support http2
+ apache2 > 2.4.10 (might get "client preface string missing or corrupt" if apache2 is old version)
2. Setup server:
+ recommend install ubuntu 16.04 which already have apache and openssl updated
+ how to install curl: https://serversforhackers.com/video/curl-with-http2-support
# Get build requirements
# Some of these are used for the Python bindings
# this package also installs
sudo apt-get install g++ make binutils autoconf automake autotools-dev libtool pkg-config \
zlib1g-dev libcunit1-dev libssl-dev libxml2-dev libev-dev libevent-dev libjansson-dev \
libjemalloc-dev cython python3-dev python-setuptools
# Build nghttp2 from source
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure
make
sudo make install
# build newest curl
sudo apt-get build-dep curl
wget http://curl.haxx.se/download/curl-7.46.0.tar.bz2
tar -xvjf curl-7.46.0.tar.bz2
cd curl-7.46.0
./configure --with-nghttp2=/usr/local --with-ssl
make
sudo make install
sudo ldconfig
#test
curl --http2 -I nghttp2.org
3. Test send push notification with curl
+ http://stackoverflow.com/questions/34684099/new-apns-provider-api-and-php
+ script:
curl -d '{"aps":{"alert":"hi","sound":"default"}}' \
--cert <your-certificate.pem>:<certificate-password> \
-H "apns-topic: <your-app-bundle-id>" \
--http2 \
https://api.development.push.apple.com/3/device/<device-token>
Woah
ReplyDelete