Rah*_*sal 5 gem ruby-on-rails heroku apple-push-notifications ios
我正在尝试从我的Rails应用程序发送推送通知.我尝试了宝石APNS,休斯顿,当我在我的开发机器上时,它们的工作非常棒.
这些宝石需要/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)
Run Code Online (Sandbox Code Playgroud)
宝石休斯顿要求PEM作为File
对象.但是,我不能这样做File.open("url_to_my_pem_file")
您可以使用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
Run Code Online (Sandbox Code Playgroud)
我最终将 AWS-S3 文件复制到 Heroku 应用程序,使用复制的版本(因为它是本地的),然后在发送通知后删除复制的文件。
\n\nfname = "tempfile.pem"\n# open the file, and copy the contents from the file on AWS-S3\nFile.open(fname, \'wb\') do |fo|\n fo.print open(AWS::S3::S3Object.url_for(LOCATION_ON_S3, BUCKET_NAME)).read\nend\nfile = File.new(fname)\n# use the newly created file as the PEM file for the APNS gem\nAPNS.pem = file \n\ndevice_token = \'<a4e71ef8 f7809c1e 52a8c3ec 02a60dd0 b584f3d6 da51f0d1 c91002af 772818f2>\'\nAPNS.send_notification(device_token, :alert => \'New Push Notification!\', :badge => 1, :sound => \'default\')\n\n# delete the temporary file\nFile.delete(fname)\n
Run Code Online (Sandbox Code Playgroud)\n\n再想一想,我可以使用私有资产,就像这个问题 \xe2\x80\x94Where to put private document to use in Rails applications? ,但即使是答案也提到 AWS-S3 可能是一个更好的主意。
\n 归档时间: |
|
查看次数: |
2999 次 |
最近记录: |