Heroku Rails App上的iOS推送通知 - 如何提供PEM文件

时间:2014-02-18 02:23:10

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

我正在尝试从我的Rails应用程序发送推送通知。我尝试了宝石APNSHouston,当我在我的开发机器上时,它们的工作非常棒。

这些宝石需要/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")

2 个答案:

答案 0 :(得分:7)

您可以使用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

答案 1 :(得分:3)

我最终将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可能是个更好的主意。