【问题标题】:iOS push notification on Heroku Rails App — how to provide the PEM fileHeroku Rails App 上的 iOS 推送通知 — 如何提供 PEM 文件
【发布时间】:2014-02-18 02:23:10
【问题描述】:

我正在尝试从我的 Rails 应用程序发送推送通知。我尝试了 gems APNS、Houston,当我在我的开发机器上时,它们工作得非常好。

这些 gem 需要 /path/to/PEM/file(Apple 的证书)来发送通知。但是,我似乎无法弄清楚如何在生产服务器上提供此文件。我正在使用 Heroku。

我尝试将它上传到 Amazon-S3(非公开)并从那里使用它。但是,这不起作用,因为 gems 会查找本地文件(而不是 URI)。如何在 Heroku 上保存本地文件?

gem APNS 需要将路径作为字符串。然后检查文件是否存在。

raise "The path to your pem file does not exist!" unless File.exist?(self.pem)

gem Houston 需要 PEM 作为 File 对象。但是,我做不到File.open("url_to_my_pem_file")

【问题讨论】:

  • 我在某种程度上取决于您发送通知的频率,但是在 S3 上托管证书意味着总是会导致一小段延迟(因为您必须将文件复制到本地环境)发送它。

标签: ios ruby-on-rails heroku gem apple-push-notifications


【解决方案1】:

您可以只使用Rails.root var 来获取本地路径。在 S3 上托管您的证书文件可能有点矫枉过正,而且您现在正在使您的推送服务器依赖于 S3。如果有停机时间,你不能推动。此外,您会因拨打网络电话而减慢速度。

这是来自我的 Rails 生产推送服务器的示例方法:

def cert_path
    path = "#{Rails.root}/config/apn_credentials/"
    path += ENV['APN_CERT'] == 'production' ? "apn_gather_prod.pem" : "apn_gather_dev.pem"
    return path    
end

【讨论】:

  • 您是如何将证书文件添加到“/config/apn_credentials/”目录的?您是否以某种方式复制了它,或者将其保存在存储库中并推送到 heroku?
【解决方案2】:

我最终将 AWS-S3 文件复制到 Heroku 应用程序,使用复制的版本(因为它是本地的),然后在发送通知后删除复制的文件。

fname = "tempfile.pem"
# open the file, and copy the contents from the file on AWS-S3
File.open(fname, 'wb') do |fo|
    fo.print open(AWS::S3::S3Object.url_for(LOCATION_ON_S3, BUCKET_NAME)).read
end
file = File.new(fname)
# use the newly created file as the PEM file for the APNS gem
APNS.pem = file 

device_token = '<a4e71ef8 f7809c1e 52a8c3ec 02a60dd0 b584f3d6 da51f0d1 c91002af 772818f2>'
APNS.send_notification(device_token, :alert => 'New Push Notification!', :badge => 1, :sound => 'default')

# delete the temporary file
File.delete(fname)

再想一想,我本可以像这个问题一样使用私有资产 — Where to put private documents to use in Rails applications?,但即使是答案也提到 AWS-S3 可能是一个更好的主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2015-03-29
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多