158
153

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsからiPhone、Androidへのpush通知メモ

Last updated at Posted at 2014-07-18

RailsからiPhoneやAndroidにpush通知した。
使ったのは下記Gem。

必要なもの

iPhone

Android

iPhoneへpush通知

Gemfileに追加後bundle install

gem 'apns'

まずpemファイルを作ってあげないといけない

openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts

作ったpemファイルを/path/to/cert.pemに配置し、下記でpush通知が送れる。

APNS.host = 'gateway.push.apple.com' 
APNS.pem  = '/path/to/cert.pem'
APNS.port = 2195 

device_token = 'xxxxxxxxxxxxxxxxxxxx' # 送りたい端末のdevice token
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')

Androidへpush通知

Gemfileに追加後bundle install

gem 'gcm'

下記実行するとandroid側に通知が飛ぶ

require 'gcm'

api_key = "xxxxxxxxxxxxxxxxxxxx"
registration_ids = ["a1", "a2", "a3"] # 送りたいregistration_idの配列
gcm = GCM.new(api_key)
options = {data: {score: "123"}, collapse_key: "updated_score"}
response = gcm.send_notification(registration_ids, options)

options内のdataハッシュ内に自由にキーを設定して送信してあげるとAndroid側で取得して色々処理できたりします

158
153
0

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
158
153

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?