Heroku Rails应用程序的iOS推送通知 - 如何提供PEM文件

5
我正在尝试从我的Rails应用程序发送推送通知。我尝试了APNSHouston这些gem,当我在开发机器上时,它们的效果非常好。
这些gems需要/path/to/PEM/file(苹果证书)才能发送通知。然而,我似乎无法弄清楚如何在生产服务器上提供此文件。我正在使用Heroku。
我尝试将其上传到Amazon-S3(非公共),并从那里使用它。但是,这不起作用,因为gems寻找本地文件(而不是URI)。我该如何在Heroku上保存本地文件?
APNS gem要求路径为字符串。它然后检查文件是否存在。
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上托管证书意味着在发送之前始终会有一些小延迟(因为您必须将文件复制到本地环境)。 - wspruijt
2个回答

7
您可以使用Rails.root变量获取本地路径。将证书文件托管在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上呢? - tommybernaciak

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)

经过再次思考,我本可以使用私有资产,就像这个问题中所提到的——在Rails应用程序中放置私有文档的位置?,但是即使答案提到AWS-S3可能是一个更好的选择。


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