I have a project that needs to send notifications from server to device. I don't know how and where to start (create table first or other), because I'm new to Ruby on Rails. I've follow the apn_sender tutorial but it won't work and always errors out on start. Is there any full tutorial to create apn_sender for Rails 3?
Thanks
Rails 3 and apn_sender
I too have been working on the apn_sender problem, because I like that the Apple Push Notification support seems pretty robust. However, the project has been abandoned and now it is falling behind the latest 3 series rails. I have gotten the system working as follows.
Gemfile
gem 'apn_sender', :require => 'apn'
gem 'daemons'
gem 'resque', '1.20.0'
gem 'resque-access_worker_from_job'
The signal handling changed in Resque after 1.20 so you must use the older 1.20.0. Daemons should have been a requirement all along but was never explicitly in the gem spec.
script/apn_sender
#!/usr/bin/env ruby
# Daemons sets pwd to /, so we have to explicitly set RAILS_ROOT
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require 'rubygems'
require 'apn'
require 'apn/sender_daemon'
APN::SenderDaemon.new(ARGV).daemonize
Retrieved this from here. The reason the apn_sender script is not generated is because the generator script needs to be updated for 3 series rails.
Starting the sender
./script/apn_sender --environment=production --verbose start
When starting the environment the production value is probably what you want since both ad_hoc and release versions are using the production certificate.
This fork on github has corrected some of the rails 3 issues, but the resque version problem still needs to be resolved explicitly.
Manually testing
This website shows how to send messages bypassing the daemon. He stores the tokens as base64 encoded strings which is nice and compact. The format of a device string in the apn_sender framework is a hex string formatted like this: deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf
. Spaces optional.
require 'base64'
ios_device_token = User.where(username: 'Cameron').first.ios_device_token
token = Base64.decode64(ios_device_token).unpack('H*').first.scan(/\w{8}/).join(' ')
notification = APN::Notification.new(token, { alert: 'Hello, World!', sound: true, badge: 99 })
sender = APN::Sender.new(verbose: true, environment: 'production')
sender.send_to_apple(notification)
Make sure the feedback.rb file is up-to-date
# Unpacking code borrowed from http://github.com/jpoz/APNS/blob/master/lib/apns/core.rb
while bunch = socket.read(38) # Read data from the socket
f = bunch.strip.unpack('N1n1H140')
feedback << APN::FeedbackItem.new(Time.at(f[0]), f[2])
end
One version of the code lacked 38 and it resulted in bad token strings being returned from the feedback service
RubyMine by JetBrains
If you plan on taking Ruby development to the next level you should also consider an IDE like RubyMine because it makes everything so much easier.
来源:https://stackoverflow.com/questions/13949624/apn-sender-and-rails-3