Push通知の実装をしていて、Pushサーバーが用意されてない時に、手軽に動作を試す時に使えそうでした。
ApnsPHPがオワコンっぽいので、代替するものを探してたところ、Houstonにたどり着きました。
準備
インストール
下記のコマンドでインストールする。
gem install houston
証明書準備
-
キーチェーンアクセスからPush通知サーバー向けの証明書を書き出す。
- パスワードなしで出力する。
- p12形式とする。
p12をpemに変換する。
openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts
ソースコード作成
下記のような感じでrubyで書く。
test_push.rb
require 'houston'
# Environment variables are automatically read, or can be overridden by any specified options. You can also
# conveniently use `Houston::Client.development` or `Houston::Client.production`.
#APN = Houston::Client.development
APN = Houston::Client.production
APN.certificate = File.read("./apple_push_notification.pem")
# An example of the token sent back when a device registers for notifications
token = "<XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX>"
# Create a notification that alerts a message to the user, plays a sound, and sets the badge on the app
notification = Houston::Notification.new(device: token)
notification.alert = "12345678901234567890123456789012345678901234567890"
# Notifications can also change the badge count, have a custom sound, have a category identifier, indicate available Newsstand content, or pass along arbitrary data.
notification.custom_data = {url: "http://www.google.com"}
# And... sent! That's all it takes.
APN.push(notification)
実行
下記のような感じで実行する。
ruby test_push.rb