Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending IOS Push Notifications from Ruby On Rails

I need to send push notifications to IOS mobile.

I used apn_on_rails for send this notifications.

I get my pem file from IOS developer. I did the following configurations on it

In Productions & development in config/configtran/*.rb

RAILS_ROOT, APN::App::RAILS_ENV='production'
configatron.apn.passphrase  = 'leo_123'
configatron.apn.port  = 2195
configatron.apn.host  = 'gateway.sandbox.push.apple.com'
configatron.apn.cert = File.join(Rails.root, 'config', ' apple_push_notification_development.pem')

And then i created one Notificationby following the documentation.

After i ran the following commands it stands ideal and responding

From console

APN::App.send_notifications

Using rake RAILS_ENV=production rake apn:notifications:deliver

I tried that in both production and development environment. After the follwing line it's not responding. I didn't found anything in Log also.

APN::App Load (0.1ms)  SELECT "apn_apps".* FROM "apn_apps"

I haven't hosted this app in any cloud server. Is this application should host in any cloud server to work the app properly?

Any help appreciated.

like image 736
santosh Avatar asked Jun 09 '14 13:06

santosh


1 Answers

It's easier to work with grocer gem.

pusher = Grocer.pusher(certificate: "#{Rails.root}/lib/dev_cert.pem", # required
                       passphrase:  "XXXXXX",                         # optional
                       gateway:     "gateway.sandbox.push.apple.com", # optional
                       port:        2195,                             #optional
                       retries:     3)

feedback = Grocer.feedback( certificate: "#{Rails.root}/lib/dev_cert.pem", 
                                  passphrase:  "XXXXXX",                        
                                  gateway:     "feedback.sandbox.push.apple.com", 
                                  port:        2196,                     
                                  retries:     3)

 notification = Grocer::Notification.new(
    device_token:      push_id,
    alert:             message,
    badge:             3,
    sound:             "siren.aiff",         # optional
    expiry:            Time.now + 60*60,     # optional; 0 is default, meaning the message is not stored
    # identifier:        1234,                 # optional
    content_available: true                  # optional; any truthy value will set 'content-available' to 1
  )

  feedback.each do |attempt|
    puts "Device #{attempt.device_token} failed at #{attempt.timestamp}"
  end

  pusher.push(notification)
like image 72
user2503775 Avatar answered Oct 02 '22 11:10

user2503775