Sending iOS push notifications from a Rails application is very easy to do these days, thankfully there are many great Ruby gems that can be used to handle most of the magic for you. Recently I ran into the apn_sender gem which handles sending push notifications in a really neat way.
Sending push notifications directly from a Rails application can be slow and we probably don’t want to have the user waiting until the notification is sent, instead apn_sender can be setup to run a worker which is constantly connected to the apple push notification service. When there are new notifications to send, the notifications are queued up and sent through the always open connection that is maintained by the worker.
apn_sender uses redis as a message queue to keep track of the notifications waiting to be sent, you’ll need to install it before using the gem.
To add apn_sender in your Rails 3 application, just add the gem to your Gemfile. We’re going to need the daemons gem too so we’ll include that as well.
Can’t see this Gist? View it on Github!
Now we can create our daemon which we will be using for sending push notifications, this can be placed anywhere, I’ve put mine in script/apn_sender. Make sure to add execute permission to the file after creating so we can run it.
Can’t see this Gist? View it on Github!
Before the daemon can start running we’ll need to put our iOS push certificate into the application. Instructions for generating the certificates are available at the Apple Developer site. The certificates need to be placed inside of /config/certs and should be named apn_development.pem or apn_production.pem for production.
Once the certificates are in their correct locations, we can start up the daemon. The daemon does not know about the Rails environment so we need to specify this when starting it up. The daemon supports start, stop and restart commands. There is a verbose flag available to output more information (which can be helpful when debugging).
Can’t see this Gist? View it on Github!
Our application is now set up to send push notifications, this can now easily be performed by adding a new notification to the queue. The notify method on the APN class will take a push notification token and then our parameters, we can specify the alert message to show the user, whether or not we want sound as well as the number to display on the badge icon. Anything else we pass to notify will be sent as metadata in the push notification. Here’s an example of creating a notification.
Can’t see this Gist? View it on Github!
The worker should pick up the notification within a few seconds and send it off. The apn_sender has many other features that I haven’t covered, you can view the full documentation over at https://github.com/kdonovan/apn_sender.
Ming
Jun 2, 2011
Hi Mario, thanks for the great gem! It is very useful work you are doing here. I have installed the gem, and ran bundle install.
However, I tried to run apn:server, and it gives me this error
rake apn:server –trace
rake aborted!
Don’t know how to build task ‘apn:server’
I have already added: require ‘apn/tasks’ to my Rakefile. Do you have any idea what might be going on?
Thanks a lot for your help!
Mario Visic
Jun 2, 2011
Hi Ming
The two rake tasks added in apn_sender are:
rake apn:sender
rake apn:senders
The first will start a single server and the second will spin up multiple.
You can view a list of the available rake tasks by running `rake -T`
Pingback: iPhone push notifications not working in Rails app – apn_sender | taking a bite into Apple
Guy Argo
Jul 7, 2011
I found your article very handy but one piece of information eluded me: how to generate the .pem file as Apple supplied a .cer file. StackOverflow to the rescue:
http://stackoverflow.com/questions/1762555/creating-pem-file-for-apns
Mario Visic
Jul 12, 2011
Thanks Guy
The instructions for generating the SSL certs are at the bottom of the apple developer article I linked to above, it’s all the way down the very bottom though and hard to spot.
Thanks for the stackoverflow link, the instructions make it much easier to figure out what needs to be done to generate a key :)
ruby_padawan
Sep 16, 2011
Hi, man!
I installed gems, create file apn_sender.rb and from rake -T can see “rake apn:sender” and “rake apn:senders”.
But I have some problems. For now I stops at:
“Connection refused – Unable to connect to Redis on 127.0.0.1:6379″
And I wasn’t being able to make
“script/apn_sender –environment=development –verbose start”
because I had:
“script/apn_sender: No such file or directory”
Maybe it’s because my understanding is not so well, but i can’t find the way to solve it.
ruby_padawan
Sep 16, 2011
UPD:
I installed redit server for my Ubuntu; gem ‘resque’; made account at redittogo and put the code inside initializer resque.rb:
require ‘resque’
ENV["REDISTOGO_URL"] ||= “redis://Name:4205bd9451670a713063c34ac64d53f1@pake.redistogo.com:9115/”
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Inside my contoller.rb : APN.notify(’3d365dc984e8643da2b86683a2f04a791b1f5c2333aaac0b3807b6bf58f10c13′, :alert => ‘spam’, :badge => 4, :sound => true, :other_information => ‘value’)
and apn_sender.rb your code :
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ‘..’))
require ‘rubygems’
require ‘apn’
require ‘apn/sender_daemon’
When I made rake:apn_sender at terminal I can see:
“*** Starting worker to send apple notifications in the background from sergey-Compaq-610:31583:apple_push_notifications”
…and then nothing going on. It’s just hanging.
What’s wrong? :(
Mario Visic
Sep 19, 2011
Hi ruby_padawan
I’m not too sure why it’s not hitting your worker at all. Perhaps try running APN.notify in the rails console to see if it works from there.
There’s more documentation that might be helpful at the gems github page over here: https://github.com/kdonovan/apn_sender
ruby_padawan
Sep 19, 2011
Yes, but it returns for me an empty array [] and worker keeping silence.
ruby_padawan
Sep 19, 2011
And when I running it from the controller on my local server I can see “authenticity_token” parameter in logs.
Mari
Nov 12, 2011
hi Mario,
I am following your instructions, but when I try:
script/apn_sender.rb –environment=development –verbose start
I get the following message:
script/apn_sender.rb: line 5: syntax error near unexpected token `(‘
script/apn_sender.rb: line 5: `RAILS_ROOT = File.expand_path( sFile.join( File.dirname(__FILE__), ‘..’))’
I copied and pasted from your git
What should I do? I am using ruby 1.9.2 and rail 3
Sharma
May 8, 2012
Hi Mario,
I have used the “apn_on_rails” gem for pushing the notifications from my rails application. I have done all the migrations regarding the apn_on_rails gem. But I am getting confused while getting the certificate for apn.
So, my question is, Can we generate the certificate from the rails application or we need to get a seperate software for getting the certificate.If so, please guide me.I have used the configatron gem too but it is throwing an error that configatron is already in use.
I am using ruby 1.8.7 and rails 2.3.11.Thanks in advance.
Netuddki
Dec 15, 2012
Or you can use http://pushwizard.com, a free service for sending push notifications to unlimited devices.
It can handle 250+ million messages per hour.
Set up and integration within minutes.
You don’t have to mess around with your own code and server.
Edy
Jan 3, 2013
Hi mario and guys . . is there anyone who can help for running daemon ? as i know ‘./script/apn_sender –environment=production –verbose start’ , but when i run that script , it show bash: ./script/apn_sender: No such file or directory … when i changed into “rails apn_sender –environment=production –verbose start’ , it show Error: Command not recognized
Usage: rails COMMAND [ARGS] , ….”
any solution ?
Thanks
prem
Jan 11, 2013
Hi, I could not send push notificaitons using apn_sender now. are there any errors to send push notifications using apn_sender gem now or is it still working fine? Any body still using this to send push notifications? please let me know, if it is working fine for any of you guys.