I am trying to send push notifications from my Rails app. I tried the gems APNS, Houston, and they work fantastic when I am on my development machine.
我正在尝试从我的Rails应用上发送推送通知。我试用了位于休斯顿的gems APNS,当我在我的开发机器上时,它们工作得非常出色。
These gems need the /path/to/PEM/file
(Apple’s certificate) to send the notifications. However, I can't seem to figure out how to provide this file on production server. I am using Heroku.
这些gem需要/path/to/PEM/file(苹果的证书)来发送通知。但是,我似乎不知道如何在生产服务器上提供这个文件。我使用Heroku。
I tried having it uploaded to Amazon-S3 (non-public) and using it from there. However, this doesn’t work because the gems look for a local file (and not an URI). How do I save a local file on Heroku?
我试着把它上传到Amazon-S3(非公开的),然后从那里开始使用。但是,这并不起作用,因为gems会查找本地文件(而不是URI)。如何在Heroku上保存本地文件?
The gem APNS requires the path as a string. It then checks if the file exists.
gem APNS需要路径作为字符串。然后检查文件是否存在。
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
The gem Houston requires the PEM as a File
object. However, I cannot do File.open("url_to_my_pem_file")
gem Houston要求PEM作为文件对象。但是,我不能执行File.open(“url_to_my_pem_file”)
2 个解决方案
#1
7
You could just use the Rails.root
var to get a local path. Hosting your cert files on S3 might be a bit overkill, and you're making your push server dependent on S3 now. If there's downtime, you can't push. Also, you're going to be slowed down by making a web call.
你可以使用Rails。获取本地路径的根var。在S3上托管您的cert文件可能有点过了头,现在您正在使您的推送服务器依赖于S3。如果有停工期,你就不能推。同时,你也会因为打个网络电话而放慢速度。
Here's a sample method from my rails production push server:
以下是我的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
#2
3
I ended up copying the AWS-S3 file to the Heroku app, use the copied version (since it is local), and then delete the copied file once the notifications were sent.
最后我将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)
I wrote a post about it — http://wp.me/p1pbh7-2j
我写了一篇关于它的帖子——http://wp.me/p1pbh7-2j。
On second thoughts, I could've used private assets like in this question — Where to put private documents to use in Rails applications?, but even the answer mentions that AWS-S3 is probably a better idea.
关于第二个想法,我可以在这个问题中使用私有资产——在Rails应用程序中使用私有文档的位置?,但即使答案也提到了AWS-S3可能是一个更好的主意。
#1
7
You could just use the Rails.root
var to get a local path. Hosting your cert files on S3 might be a bit overkill, and you're making your push server dependent on S3 now. If there's downtime, you can't push. Also, you're going to be slowed down by making a web call.
你可以使用Rails。获取本地路径的根var。在S3上托管您的cert文件可能有点过了头,现在您正在使您的推送服务器依赖于S3。如果有停工期,你就不能推。同时,你也会因为打个网络电话而放慢速度。
Here's a sample method from my rails production push server:
以下是我的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
#2
3
I ended up copying the AWS-S3 file to the Heroku app, use the copied version (since it is local), and then delete the copied file once the notifications were sent.
最后我将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)
I wrote a post about it — http://wp.me/p1pbh7-2j
我写了一篇关于它的帖子——http://wp.me/p1pbh7-2j。
On second thoughts, I could've used private assets like in this question — Where to put private documents to use in Rails applications?, but even the answer mentions that AWS-S3 is probably a better idea.
关于第二个想法,我可以在这个问题中使用私有资产——在Rails应用程序中使用私有文档的位置?,但即使答案也提到了AWS-S3可能是一个更好的主意。