Ionic Push using Ruby

Greeting and salutations everyone!

I’m using the new ionic push service, it works great! I have tested it using the CLI and the Dash, and now I’m trying to build a module within my server (Ruby on Rails) to send the notifications.

The docs have a pretty neat example of how to do it using Python and urllib2. My question is: Does anybody have the equivalent example using Ruby?

I was thinking httparty or Net/Http would probably do the trick, but I don’t know how to correctly assemble the POST request (with all the params, etc)

Thanks in advance for your help,
-J

Hey there. So you may want to ask in our io-services gitter chat room. https://gitter.im/driftyco/ionic-io-testers

1 Like

Thank you @mhartington, I was actually able to solve the problem using Httparty!

1 Like

@jlstr do you have a code example of the call you made using HTTParty?

Thanks!

Absolutely, here you go:

module Ionic
class PushService
include HTTParty
base_uri ‘https://push.ionic.io

def initialize user_id, device_token
  @auth = { username: ENV['IONIC_APP_SECRET_KEY'] }
  @headers =  { 'Content-Type' => 'application/json', 'X-Ionic-Application-Id' => ENV['IONIC_APP_ID'] }
  @device_token = device_token
end
def one_time_notification message
  raise Exception.new 'device token not found' and return if @device_token.nil?
  options = {}
  body = { :'tokens' => [ @device_token ], :'notification' => { :'alert' => message } }.to_json
  options.merge!({ :body => body }).merge!({ :basic_auth => @auth }).merge!({ :headers => @headers })
  self.class.post('/api/v1/push', options)
end

end
end

Then I call it like this:

Ionic::PushService.new(user_id, device_token).one_time_notification ‘The message’

Please be kind to change the module/class names, this is my production code :smile:

Have a nice day!

1 Like

Thanks so much, yeah absolutely, really just having a problem authenticating and wanted to see how someone else was doing it in ruby. Thanks so much!

1 Like

No problem!

Actually, Let me know how it goes because the code I’ve posted there is working well, but I’m having an issue where not all the users that I register for push using $ionicPush are successfully receiving/generating an ios device_token. Let me know If you stumble upon the same problem.

Kind Regards,

Will do, I am pretty early on in development but have this thread bookmarked now and will report back soon. Cheers!

Update: the problem I mentioned appears to be related to the apple certificate only. That’s good and bad news I guess.

yeah, so far, i haven’t ran into any problems. Thanks for the info though

Im Very happy now :’)
So many Thanks :blush: :wink:

1 Like