Push notification using Urban AirShip and Rails 3 for iphone

What is the best way to send push notifications via Urban AirShip using the Rails 3 web application?

From the documentation for the city airship:

HTTP POST to / api / push / broadcast / sends this notification to all registered device tokens for expression. The payload is in JSON using a content type / json application, with this structure:

{ "aps": { "badge": 15, "alert": "Hello from Urban Airship!", "sound": "cat.caf" }, "exclude_tokens": [ "device token you want to skip", "another device token you want to skip" ] } 

I really don't know where to start!

+7
source share
4 answers

It is best to use the UrbanAirship Groupon version . Everything is clearly indicated in their documents, and it will be much more accurate. Worked and tested in my application.

From the read me file (with a few comments and all): -

Note. If you are using Ruby 1.8, you should also set git system_timer for more reliable timeout behavior. See http://ph7spot.com/musings/system-timer for details. Baically

  gem install system_timer 

Installation

  gem install urbanairship # or specify in your gem file gem "urbanairship" 

The configuration defines all this in the initialization directory, and then make sure that you restart the application.

  Urbanairship.application_key = 'application-key' Urbanairship.application_secret = 'application-secret' Urbanairship.master_secret = 'master-secret' Urbanairship.logger = Rails.logger Urbanairship.request_timeout = 5 # default 

Using

Register device token

  Urbanairship.register_device 'DEVICE-TOKEN' # => true 

Unregister device token

  Urbanairship.unregister_device 'DEVICE-TOKEN' # => true 

Sending push notifications (for instant delivery, remove the schedule_for attribute)

  notification = { :schedule_for => 1.hour.from_now, :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'], :aps => {:alert => 'You have a new message!', :badge => 1} } Urbanairship.push notification # => true 

Sending push notifications send (remove the schedule_for attribute for instant delivery)

  notifications = [ { :schedule_for => 1.hour.from_now, :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'], :aps => {:alert => 'You have a new message!', :badge => 1} }, { :schedule_for => 3.hours.from_now, :device_tokens => ['DEVICE-TOKEN-THREE'], :aps => {:alert => 'You have a new message!', :badge => 1} } ] Urbanairship.batch_push notifications # => true 

Sending Broadcast Notifications Urbanairship allows you to send broadcast notifications to all active registered device tokens for your application (remove the schedule_for attribute for instant delivery)

  notification = { :schedule_for => 1.hour.from_now, :aps => {:alert => 'Important announcement!', :badge => 1} } Urbanairship.broadcast_push notification # => true 
+23
source
0
source

Ok, this is an easy way to send a city airship broadcast from the ROR controller:

 require 'net/http' require 'net/https' require 'open-uri' app_key = 'JJqr...' app_secret = 'lAu7...' master_secret = 'K81P...' payload ={ "aps" => {"badge" => "0", "alert" => "My own message", "sound" => ""} }.to_json full_path = 'https://go.urbanairship.com/api/push/broadcast/' url = URI.parse(full_path) req = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json'}) req.body = payload req.basic_auth app_key, master_secret con = Net::HTTP.new(url.host, url.port) con.use_ssl = true r = con.start {|http| http.request(req)} logger.info "\n\n##############\n\n " + "Resonse body: " + r.body + " \n\n##############\n\n" 

Hooray!

0
source

In Rails 4.2.x and Ruby 2.3.x I use urbanairship 'gem, best referenced in the latest documentation found here .

Sending example in ios:

 def deliver_push_notification recipient_uuid, message ua = Urbanairship client = ua::Client.new(key: ENV["URBAN_AIRSHIP_APP_KEY"], secret: ENV["URBAN_AIRSHIP_MASTER_SECRET"]) p = client.create_push p.audience = ua.ios_channel(recipient_uuid) p.notification = ua.notification(ios: ua.ios(alert: message)) p.device_types = ua.device_types(["ios"]) p.send_push end 
0
source

All Articles