Sunday, August 12, 2012

Rails 3 : APN on Rails

I got very excited when one of the colleague told me that he wants to send notifications to his apple devises(itouch/ iphone) through rails application I am working on.

So I got PRX/apn_on_rails and yekmer.posterous.com tutorial. And I started working after reading the required information. Wow..

But there was error as below in console while running devise creation command
ActiveRecord::StatementInvalid: PG::Error: ERROR:  relation "apn_bases" does not exist
LINE 4:              WHERE a.attrelid = '"apn_bases"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"apn_bases"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Well, this was not working.
Tried to find some solution, and got issue 38 for github.com/PRX/apn_on_rails.

After reading added
self.abstract_class = true
to apn/base.rb and restarted the server. It started working as required. It successfully created a devise information row in the table.

I thought the work is done(not really) and just need to run command
rake apn:notifications:deliver --trace
But it started throwing the error as below
** Invoke apn:notifications:deliver (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute apn:notifications:deliver
rake aborted!
SSL_connect returned=1 errno=0 state=SSLv3 read finished A: sslv3 alert certificate unknown
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/libs/connection.rb:58:in `connect'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/libs/connection.rb:58:in `open'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/libs/connection.rb:23:in `open_for_delivery'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/app/models/apn/notification.rb:104:in `send_notifications'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/tasks/apn.rake:7:in `block (3 levels) in '
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
Again searched and got to know that there might be something to do with certificate generated. Ok, I and colleague discussed on this and he solved the same with generating new .pem file for this task. The certificate generation steps can be viewed at maniacdev.com/.

Replaced the same in my project and wow, that worked like a charm.

12 comments:

  1. Hi, Supriya, thanks for the blog, it helped me a lot. I have a question about the issue #38, how did you resolve it? I installed the apns by GEM, I am not sure where can i find the apn_rails source code to fix it.

    ReplyDelete
    Replies
    1. Hi,

      That actually fixed the issue, but its kind of patch or quick fix.

      Still for now you can find base.rb in home/.rvm/current_gemset/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/app/models/apn/base.rb

      Delete
    2. Thank you Supriya. But I am using Heroku to deploy our site, in this case, is it possible to do the "quick fix"?

      Delete
    3. Yes, thats the problem. I checked the gem for previous version of rails and it shows the code updated but not for rails 3 branch.

      I have forked the same rails 3 branch and updated the code.
      Please check the below link if it can help you for the heroku app.

      https://github.com/supriyassurve/apn_on_rails

      Thanks :)

      Delete
    4. Thanks, it works, I can insert data to the DB table. But I got a new issue, when I fire "rake apn:notifications:deliver", it is said:undefined method `to_hexa' for nil:NilClass, have you met that?

      Delete
    5. Here is full log:

      undefined method `to_hexa' for nil:NilClass
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/app/models/apn/notification.rb:82:in `message_for_sending'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/app/models/apn/notification.rb:106:in `block (2 levels) in send_notifications'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/app/models/apn/notification.rb:105:in `each'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/app/models/apn/notification.rb:105:in `block in send_notifications'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/libs/connection.rb:60:in `open'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/libs/connection.rb:23:in `open_for_delivery'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/app/models/apn/notification.rb:104:in `send_notifications'
      /Users/lzbob/.bundler/ruby/1.9.1/apn_on_rails-b7f6df233ea0/lib/apn_on_rails/tasks/apn.rake:7:in `block (3 levels) in '
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
      /Users/lzbob/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/bin/rake:33:in `'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/bin/rake:19:in `load'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125@global/bin/rake:19:in `'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `eval'
      /Users/lzbob/.rvm/gems/ruby-1.9.3-p125/bin/ruby_noexec_wrapper:14:in `'

      Delete
    6. Hi,

      I didn't face this issue, but according to the log you added, it seems that device is nil, for notification you are sending. Hope you have defined device before sending it.

      Delete
  2. Hi,

    I'm currently battling the same "SSL_connect returned=1 errno=0 state=SSLv3 read finished A: sslv3 alert certificate unknown" ... I followed the steps from the apn_on_rails readme to generate the .pem file so I don't know where else to look. You say your issue was resolved by regenerating the file, can you post the steps you followed? I went to the blog you linked to but couldn't find those steps.

    Thanks,
    Jose

    ReplyDelete
    Replies
    1. Hi,

      I am really sorry for that link. I have rectified the post with the correct link which is maniacdev.com. Here you can find the post and the links of tutorial which will help you for certificate creation.

      Hope this helps.

      Delete
  3. Hi,
    While running rails g apn_migrations command in my rails 3 project i have got following error..

    /.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails/apn_on_rails.rb:64:in `rescue in block in ': uninitialized constant Dependencies (NameError)
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails/apn_on_rails.rb:60:in `block in '
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails/apn_on_rails.rb:56:in `each'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails/apn_on_rails.rb:56:in `'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails.rb:2:in `block in '
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails.rb:1:in `each'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/bundler/gems/apn_on_rails-ae314f5faa18/lib/apn_on_rails.rb:1:in `'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler/runtime.rb:68:in `require'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler/runtime.rb:66:in `each'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler/runtime.rb:66:in `block in require'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler/runtime.rb:55:in `each'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler/runtime.rb:55:in `require'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/bundler-1.2.2/lib/bundler.rb:128:in `require'
    from /home/amits/work-space/21112012/Punchme_web_MAIN_3002/config/application.rb:7:in `'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands.rb:24:in `require'
    from /home/amits/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands.rb:24:in `'
    from script/rails:6:in `require'
    from script/rails:6:in `'

    But, I fixed the problem by adding following line in my gem file.

    gem 'apn_on_rails', :git => 'https://github.com/supriyassurve/apn_on_rails.git', :branch => 'rails3'

    and by running Bundle Install command.

    ReplyDelete
  4. Hi Supriya,

    I keep seeing the "do not open and close connections to apple's gateway," frequently in the docs for apn. My question is, have you ever heard of apple blocking an web app for doing too many connections? I plan to implement apn on rails and will be using notifications constantly and trying to send them most likely off a cron job.

    ReplyDelete
  5. Hi Sam,

    Here is the answer of your question, hope it help you
    apple-push-notification-limitation
    Please let me know if you have any quries.

    ReplyDelete