如何从Rails应用程序发送C2DM消息

3

编辑 3

我使用 RhoMobile 获取设备令牌,然后将其传送到服务器。

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

已经有设备令牌但是一直收到“Error=NotRegistered”的错误信息。

编辑

好的,首先犯了一个错误,使用了错误的设备ID。现在已经解决了,但手机没有弹出消息提醒。

原始内容

第一次从Rails服务器发送安卓推送消息的用户。

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

我已经向Google注册并收到白名单电子邮件。

尝试运行下面的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) 

安卓代码怎么样? - aromero
我从Rhomobile函数中获取令牌。更新了带有代码的问题。 - pcasa
搞定了。手机设置有问题。 - pcasa
你是如何在手机上解决的?我也在尝试向Rhodes Android应用程序发送推送通知。谢谢。 - marimaf
1个回答

1

尝试添加一个名为Push的控制器,在控制器内部需要有以下内容:

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

至少对于我的应用程序来说,我需要让应用程序运行才能收到消息。


在Settings文件夹下的controller.rb中,不需要另外添加一个控制器,只需将其加入即可。 - pcasa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接