如何从rails应用程序发送c2dm消息

发布于 2024-12-01 12:24:02 字数 1647 浏览 9 评论 0原文

编辑 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。现在可以使用,但手机没有消息提醒。

原创

第一次从 Rails 服务器发送 android Push 消息的用户。

使用 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 = "[email protected]"
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) 

EDIT 3

I use RhoMobile to get device_token, which I am getting to server

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

EDIT 2

Have device_token but keep getting Error=NotRegistered

EDIT

OK, so first mistake was using the wrong device ID. Now working, but phone not alerting with message.

ORIGINAL

First time user of sending android Push messages from rails server.

Using https://github.com/sghael/speedy_c2dm

I registered with google and received white list email.

trying test.rb below, but nothing gets sent to phone.

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 = "[email protected]"
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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-12-08 12:24:02

尝试添加一个名为 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", ''

此外,您是否配置了构建.yml 正确吗?

android:
  push:
    notification: always
    sender: [email protected]
capabilities:
  - push
  - vibrate

至少对于我的应用程序,我需要运行应用程序才能获取消息。

Try adding a Controller named Push and inside the controller you need to have the following:

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

you need to set your application to call this by adding the following line to your application.rb

System.set_push_notification "/app/Push/push_callback", ''

Also, did you configure your build.yml correctly?

android:
  push:
    notification: always
    sender: [email protected]
capabilities:
  - push
  - vibrate

At least with my app, I need to have the application running in order to get the message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文