2014-02-18 30 views
5

我正嘗試從我的Rails應用發送推送通知。我嘗試了寶石APNS,Houston,當我在開發機器上時,它們工作得很好。Heroku Rails應用上的iOS推送通知 - 如何提供PEM文件

這些寶石需要/path/to/PEM/file(Apple的證書)發送通知。但是,我似乎無法弄清楚如何在生產服務器上提供此文件。我正在使用Heroku。

我試過讓它上傳到Amazon-S3(非公開)並從那裏使用它。但是,這不起作用,因爲寶石尋找本地文件(而不是URI)。如何在Heroku上保存本地文件?

gem APNS需要路徑作爲字符串。然後它檢查文件是否存在。

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

寶石休斯頓需要PEM作爲File對象。但是,我不能這樣做File.open("url_to_my_pem_file")

+0

我要看你通知要發送,而是託管在S3手段證書的頻率有點總會引起一個小的延遲(因爲您必須將文件複製到本地環境)才能發送。 – wspruijt

回答

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) 

我寫了一篇關於它 - http://wp.me/p1pbh7-2j

關於第二個想法,我能在這個問題上已經使用私有資產一樣 - Where to put private documents to use in Rails applications?,但即使答案中提到,AWS-S3可能是一個更好的理念。

7

您可以使用Rails.root var來獲取本地路徑。在S3上託管你的證書文件可能有點矯枉過正,你現在正在使你的推送服務器依賴於S3。如果有宕機,你不能推。此外,你會通過網絡電話的速度放慢速度。

下面是從我軌生產推送服務器的樣品的方法:

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 
+0

我不會太擔心S3的依賴性和停機時間...... – Antoine

+0

您是如何將證書文件添加到「/ config/apn_credentials /」目錄的?你有沒有複製它,或者你把它保存在存儲庫中,並推動到heroku? – tommybernaciak