LoginSignup
15
16

More than 5 years have passed since last update.

ruby から gcm を使って android 端末へメッセージを送信する

Last updated at Posted at 2014-01-05

テスト用のスクリプトです。頻繁にこういうかき捨てのコードを書くのでメモしておきます。

準備

簡単のため、https://github.com/spacialdb/gcm を利用。(基本的にはここの readme のままです。)
まずは gem をインストールしておく。

gem install gcm

先に Google API Console にアクセスし、「Google Cloud Messaging for Android」を有効にしておく必要がある。メッセージを送信するためには、さらに「認証情報」から Server Key を発行する。Project Number と Server Key が一致しないとメッセージが送信できない。

実装

以下のコードを gcm_test.rb として作成する。

gcm_test.rb
#-*- coding:utf-8 -*-
require 'gcm'
require 'pp'
# Google API Console で発行する API KEY を指定する
API_KEY="fugafuga"
gcm = GCM.new(API_KEY)

# 複数の registration id を指定できる
registration_ids = [ 'testtest' ]

# :data の内容が extras に格納される
option = { :data => {'message' => 'Hello Gcm!'} }
response = gcm.send_notification(registration_ids, option)

pp response

if response[:body]
  pp JSON.parse(response[:body])
end

実行

実行すると、以下のように json が帰る。この出力は Google Cloud Messaging のサーバが返しているレスポンスである。 success:1 と書いてあるが、送信しようとしたIDが無効だった場合など、failure:0 の部分が変化する。

$ ruby gcm_test.rb
{:response=>"success",
 :body=>
  "{\"multicast_id\":11111111111111111111,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:111111111111111111111111111111111\"}]}",
 :headers=>
  {"content-type"=>["application/json; charset=UTF-8"], "date"=>["Mon, 06 Jan 2014 00:31:24 GMT"], "expires"=>["Mon, 06 Jan 2014 00:31:24 GMT"], "cache-control"=>["private, max-age=0"], "x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "server"=>["GSE"], "alternate-protocol"=>["443:quic"], "connection"=>["close"]},
 :status_code=>200}
{"multicast_id"=>1111111111111111111,
 "success"=>1,
 "failure"=>0,
 "canonical_ids"=>0,
 "results"=>[{"message_id"=>"0:1111111111111111111111"}]}

Android の端末側では、以下のようにメッセージが受け取れる。

01-06 08:31:36.846: D/GcmShow(20042): String = null
01-06 08:31:36.846: D/GcmShow(20042): Uri = null
01-06 08:31:36.846: D/GcmShow(20042): IntentType = null
01-06 08:31:36.846: D/GcmShow(20042): IntentAction = com.google.android.c2dm.intent.RECEIVE
01-06 08:31:36.846: D/GcmShow(20042): Extra[message] = Hello Gcm!
01-06 08:31:36.846: D/GcmShow(20042): Extra[from] = 785728092392
01-06 08:31:36.846: D/GcmShow(20042): Extra[collapse_key] = do_not_collapse

なお、Intent のダンプにはいい方法が思いつかなかったのでこんなベタな方法を利用している。

Util.java
    public static void logIntent(String tag, Intent arg0){
        Log.d(tag, "String = " + arg0.getDataString());
        Log.d(tag, "Uri = " + arg0.getData());
        Log.d(tag, "IntentType = " + arg0.getType());
        Log.d(tag, "IntentAction = " + arg0.getAction());
        for(String key : arg0.getExtras().keySet()){
            Log.d(tag, "Extra[" + key + "] = " + arg0.getExtras().getString(key));
        }       
    }
15
16
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
15
16