Apn_sender and Rails 3

I have a project that should send notifications from the server to the device. I donโ€™t know how and where to start (create a table first or another), because I'm new to Ruby on Rails. I follow the apn_sender tutorial, but it will not work and will always be a startup error. Is there a complete tutorial for creating apn_sender for Rails 3?

thanks

+4
source share
1 answer

Rails 3 and apn_sender

I also worked on the apn_sender problem because I like that Apple Push Notification support seems pretty reliable. However, the project was abandoned, and now it is behind the last rails of series 3. I got the system to work as follows.

Gemfile

gem 'apn_sender', :require => 'apn' gem 'daemons' gem 'resque', '1.20.0' gem 'resque-access_worker_from_job' 

Signal processing has changed in Resque after 1.20, so you should use the older 1.20.0. Demons were supposed to be requirements all the time, but were never explicitly specified in the specification of gems.

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 

Got it from here. The reason the apn_sender script is not being generated is because the script generator needs to be updated for the 3 series of rails.

Sender Launch

 ./script/apn_sender --environment=production --verbose start 

When you start the environment, you probably need the production value, since ad_hoc and release versions use the production certificate.

This fork on github fixed some problems with rails 3, but the problem with resque problem still needs to be resolved explicitly.

Manual testing

This website shows how to send messages bypassing the daemon. It stores markers as base64 encoded strings, which are nice and compact. The device string format in the apn_sender structure is a hexadecimal string, formatted as follows: deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf . Seats are 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 feedback.rb is updated.

 # 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 

There was no 38 in one version of the code, and this led to the fact that bad token strings were returned from the feedback service

RubyMine by JetBrains

If you plan to take Ruby development to the next level, you should also consider an IDE, such as RubyMine , because it makes everything a lot easier.

+4
source

All Articles