【问题标题】:NameError: uninitialized constant Rpush::Gcm I'm trying to use rpush for my mobile app. But I keep getting this errorNameError: uninitialized constant Rpush::Gcm 我正在尝试将 rpush 用于我的移动应用程序。但我不断收到此错误
【发布时间】:2015-02-15 22:24:32
【问题描述】:

所以我尝试使用 rpush 来使用这个 gem 来推送我的移动应用程序的通知:https://github.com/rpush/rpush。我正在使用 sinatra 框架。但是我一直收到这个错误,即使我在文件顶部写了 --> require 'rpush' 。有红宝石经验的人可以帮助我吗?我是红宝石的新手,所以请多多包涵。这是我的代码

require 'rpush'

Module Notifier

def rpush_client
app = Rpush::Gcm::App.new
app.name = "App-Name"
app.auth_key = "XXXXXXXXXXXXXXX"
app.connections = 1
app.save!
end


def notify(user_id,alert)
  rpush_client
  session = db_find_one('dbname.sessions',{user_id: user_id})
  if session.present?
    device = session['devices'].first
    token = device['device_token']
    n = Rpush::Gcm::Notification.new
    n.app = Rpush::Gcm::App.find_by_name("App-Name")
    n.registration_ids = ["token", token]
    n.data = { message: alert }
    n.save!

    Rpush.push
  end
end

end

我知道这是一个愚蠢的问题,但厌倦了在这里寻找它。

【问题讨论】:

    标签: android notifications gem sinatra push


    【解决方案1】:

    我做错了什么

    就我而言,我刚刚安装了 rpush

    bundle add rpush
    

    遇到了同样的错误。

    我应该做的事情

    正如README.md 中明确提到的,您必须在将 rpush 添加到 Gemfile 后运行以下命令:

    $ bundle
    $ bundle exec rpush init
    

    正确安装会得到什么

    这将生成config/initializers/rpush.rb 和(大量)迁移文件。

    您可能希望在此之后运行迁移。

    提示

    我推荐使用rpush-redis

    您无需使用它运行额外的迁移。您可以安全地摆脱垃圾迁移文件。

    config.redis_options 可能类似于以下内容:

    Rpush.configure do |config|
    
      # Supported clients are :active_record and :redis
      config.client = :redis
    
      # Options passed to Redis.new
      config.redis_options = {
        url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/0'),
        password: ENV.fetch('REDIS_PASSWORD', '')
      }
    end
    

    【讨论】:

      【解决方案2】:

      您是否在 Sinatra 中使用捆绑程序?如果是这样,您不需要明确要求 Rpush。见这里:http://bundler.io/sinatra.html

      【讨论】:

      • 如果我删除“require 'rpush'”,它会给我一个错误,说 Uninitialized constant Rpush。我有一种感觉,我需要输入“require 'rpush/gcm'”之类的东西。但是我试过了,还是不行
      • 嘿,多年后你知道了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2019-11-22
      相关资源
      最近更新 更多