如何从rails app发送c2dm消息

时间:2011-08-26 17:56:52

标签: ruby-on-rails-3 push-notification android-c2dm rhomobile

编辑3

我使用RhoMobile来获取device_token,我将其转到服务器

def register_device
    @person = Person.find(:first)
    unless System.get_property('is_emulator')
     url = "#{@base}/users/#{@person.remote_id}/register_device?os=#{System.get_property('platform')}&device_token=#{System.get_property('device_id')}"
     Rho::AsyncHttp.get(
        :url => url,
        :headers => {'User-Agent' => Rho::RhoConfig.user_agent}
      )
    end
    finish_and_go_to_venue
  end

编辑2

拥有device_token但不断获得Error=NotRegistered

修改

好的,所以第一个错误是使用了错误的设备ID。现在正在工作,但手机没有提示信息。

ORIGINAL

首次从rails服务器发送Android Push消息的用户。

使用https://github.com/sghael/speedy_c2dm

我在谷歌注册并收到了白名单电子邮件。

尝试下面的test.rb,但没有任何内容被发送到手机。

require 'rubygems'
require 'bundler'
Bundler.setup(:default, :development)
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'speedy_c2dm'


TEST_EMAIL = "my_push_email@gmail.com"
TEST_PASSWORD = "MY_PASSWORD"
TEST_REGISTRATION_ID = "DEVICE_TOKEN_RECEIVED_FROM_PHONE"

speedyC2DM = SpeedyC2DM::API.new(TEST_EMAIL, TEST_PASSWORD)

options = {
  :registration_id => TEST_REGISTRATION_ID,
  :message => "Hi!",
  :extra_data => 42,
  :collapse_key => "some-collapse-key"
}


response = speedyC2DM.send_notification(options) 

1 个答案:

答案 0 :(得分:1)

尝试添加名为Push的Controller,并在控制器内部进行以下操作:

require 'rho/rhocontroller'
require 'helpers/browser_helper'

class PushController < Rho::RhoController
  include BrowserHelper

  def push_callback

    Alert.show_popup @params['message'] #this will show you a popup with your push message
    puts "push_callback : #{@params}" #you'll see what you are receiving with this
    "rho_push"  #to run rhodes push command processing (for alerts, sounds, etc…)
  end


end

您需要将应用程序设置为通过将以下行添加到 application.rb

来调用此方法
System.set_push_notification "/app/Push/push_callback", ''

另外,您是否正确配置了build.yml?

android:
  push:
    notification: always
    sender: your-sender-email@gmail.com
capabilities:
  - push
  - vibrate

至少在我的应用程序中,我需要运行应用程序才能收到消息。