LoginSignup
198

More than 3 years have passed since last update.

macOS High SierraのcURLで手軽にPUSH通知が送れるようになった

Last updated at Posted at 2017-09-26

macOSをHigh Sierraにアップデートしたら、cURLのバージョンが7.54.0になっており、さらにHTTP/2の利用ができるようになっていました。

iOS10からはAPNs Provider APIを利用したPUSH通知が受け取れていたため、pemファイルさえあればローカルのmacOSからiPhone実機へ向けたPUSH通知を送れます。

念のため、curlコマンドからnghttp2込みでビルドされているのを確認するのは-Vです。

$ curl -V
curl 7.54.0 (x86_64-apple-darwin17.0) libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy

Dockerを使った記事である気軽にプッシュ通知のテストがしたい【iOS10以降対応】を参考にして、macOSのcURLからAPNs Provider APIを利用するスクリプトapns.shを用意する例を示すと、下記のようになります。

$ apns.sh key.pem iPhone実機のデバイストークン

これは引数でpemファイルのパス、デバイストークンを指定するようにしています。

apns.sh
#!/bin/sh

pemfile=$1
token=$2
pushData='{"aps": {"alert": "push dev test"}}'

bundleIdentifier="アプリのBundle ID"

appleUri="https://api.development.push.apple.com/3/device/"

curl -v \
-d "$pushData" \
-H "apns-priority: 10" \
-H "apns-expiration: 0" \
-H "apns-topic: ${bundleIdentifier}" \
--http2 \
--cert ${pemfile} \
${appleUri}${token}

iOSアプリを開発するmacOS標準で用意してくれているcurlコマンドさえ叩けばいいという利点は大きいので、例えばプラグイン作ってmacアプリからPUSH通知呼び出すとか。ちょっと工夫すればいろんな遊びがありそうですね。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
198