Airship Ruby Library
Ruby library for using Airship’s messaging platform and related features.
Resources
Installation
If you do not yet have the bundler
gem, get it using:
Install Bundler
gem install bundler
Once you have the bundler
gem, add this line to your application’s
Gemfile:
Install our gem
gem 'urbanairship'
And then execute:
Bundle
bundle
Or install it yourself as:
Alternative installation
gem install urbanairship
Configuration
In your app initialization, you can do something like the following:
Example configuration
require 'urbanairship'
Urbanairship.configure do |config|
config.server = 'api.asnapieu.com'
config.oauth_server = 'oauth2.asnapieu.com'
config.log_path = '/path/to/your/logfile'
config.log_level = Logger::WARN
config.timeout = 60
end
If you want to use a custom logger such as Rails.logger, you can do:
Custom logger
require 'urbanairship'
Urbanairship.configure do |config|
config.custom_logger = Rails.logger
config.log_level = Logger::WARN
end
Available configurations
Allowances per configuration:
- log_path — Allows defining the folder where the log file will be created. The default is nil.
- log_level — Allows defining the log level. Only messages at that level or higher will be printed. The default is INFO.
- server — Allows defining the Airship server you want to use (“api.asnapieu.com” for EU or “api.asnapius.com” for US)
- oauth_server — Allows defining the Airship OAuth server you want to use. Use
oauth2.asnapieu.com
for EU oroauth2.asnapius.com
for US. - timeout — Allows defining the request timeout in seconds. The default is 5.
Usage
Broadcast to all devices:
Broadcast
require 'urbanairship'
UA = Urbanairship
airship = UA::Client.new(key:'application_key', secret:'master_secret')
p = airship.create_push
p.audience = UA.all
p.notification = UA.notification(alert: 'Hello')
p.device_types = UA.device_types(['ios','android'])
p.send_push
Basic tag push:
Tag push
require 'urbanairship'
UA = Urbanairship
airship = UA::Client.new(key:'application_key', secret:'master_secret')
p = airship.create_push
p.audience = UA.tag('some_tag')
p.notification = UA.notification(alert: 'Hello')
p.device_types = UA.device_types(['ios','android'])
p.send_push
Specify the Airship server used to make your requests
By default, the request will be sent to server api.asnapius.com:
Default server with Basic auth
require 'urbanairship'
Urbanairship::Client.new(key:'application_key', secret:'master_secret')
You can change the server globally in the Urbanairship configuration:
Using a different server
require 'urbanairship'
Urbanairship.configure do |config|
config.server = 'api.asnapieu.com'
end
Urbanairship::Client.new(key:'application_key', secret:'master_secret')
# request will be sent to the 'api.asnapieu.com' server
Using Bearer Token auth
Bearer token authentication
require 'urbanairship'
UA = Urbanairship
airship = UA::Client.new(key:'application_key', token:'token')
# Then continue as you would otherwise
Using OAuth
OAuth
require 'urbanairship'
UA = Urbanairship
app_key = 'application_key'
oauth = UA::Oauth.new(
client_id: 'client_id',
key: app_key,
assertion_private_key: 'your_private_key',
scopes: ['psh', 'chn'], # Optional
ip_addresses: ['23.74.131.15/22'], # Optional
oauth_server: 'api.asnapieu.com' # Optional
)
airship = UA::Client.new(key: app_key, oauth: oauth)
# Then continue as you would otherwise
- You cannot use both OAuth and Bearer Token auth at the same time.
- OAuth cannot be used with the older
api.urbanairship.com
andapi.airship.eu
base URLs. See Base URL and OAuth in the Airship API reference. - OAuth is not supported for all endpoints. See the Airship API authorization reference.
Feedback
Was this page helpful?
Thank you
Thanks for your feedback!
Tell Us MoreThank you
We will try harder!
Tell Us MoreCategories