urbanairship 2.4.1 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown DELETED
@@ -1,369 +0,0 @@
1
- Urbanairship is a Ruby library for interacting with the [Urban Airship API](http://urbanairship.com).
2
-
3
- Installation
4
- ============
5
- gem install urbanairship
6
-
7
- Note: if you are using Ruby 1.8, you should also install the ```system_timer``` gem for more reliable timeout behaviour. See http://ph7spot.com/musings/system-timer for more information.
8
-
9
- Configuration
10
- =============
11
- ```ruby
12
- Urbanairship.application_key = 'application-key'
13
- Urbanairship.application_secret = 'application-secret'
14
- Urbanairship.master_secret = 'master-secret'
15
- Urbanairship.logger = Rails.logger
16
- Urbanairship.request_timeout = 5 # default
17
- ```
18
-
19
- Usage
20
- =====
21
-
22
- Registering a device token
23
- --------------------------
24
- ```ruby
25
- Urbanairship.register_device('DEVICE-TOKEN')
26
- ```
27
- You can also pass an alias, and a set of tags to device registration.
28
- ```ruby
29
- Urbanairship.register_device('DEVICE-TOKEN',
30
- :alias => 'user-123',
31
- :tags => ['san-francisco-users']
32
- )
33
- ```
34
-
35
- Unregistering a device token
36
- ----------------------------
37
- ```ruby
38
- Urbanairship.unregister_device('DEVICE-TOKEN')
39
- ```
40
-
41
- Retrieving Device Info
42
- ----------------------------
43
- ```ruby
44
- Urbanairship.device_info('DEVICE-TOKEN')
45
- ```
46
-
47
- Sending a push notification
48
- ---------------------------
49
- ```ruby
50
- notification = {
51
- :schedule_for => [1.hour.from_now],
52
- :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
53
- :aps => {:alert => 'You have a new message!', :badge => 1}
54
- }
55
-
56
- Urbanairship.push(notification) # =>
57
- # {
58
- # "scheduled_notifications" => ["https://go.urbanairship.com/api/push/scheduled/123456"]
59
- # }
60
- ```
61
-
62
- If you wish to use v3 of the Urbanairship API, just add `version: 3` as an option:
63
-
64
- ```ruby
65
- Urbanairship.push(notification, version: 3)
66
- ```
67
- ### Using aliases instead of device tokens ###
68
-
69
- ```ruby
70
- notification = {
71
- :schedule_for => [1.hour.from_now],
72
- :aliases => ['ALIAS-ONE', 'ALIAS-TWO'],
73
- :aps => {:alert => 'You have a new message!', :badge => 1}
74
- }
75
-
76
- Urbanairship.push(notification) # =>
77
- # {
78
- # "scheduled_notifications" => ["https://go.urbanairship.com/api/push/scheduled/123456"]
79
- # }
80
- ```
81
-
82
- Batching push notification sends
83
- --------------------------------
84
- ```ruby
85
- notifications = [
86
- {
87
- :schedule_for => [{ :alias => 'deadbeef', :scheduled_time => 1.hour.from_now }],
88
- :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
89
- :aps => {:alert => 'You have a new message!', :badge => 1}
90
- },
91
- {
92
- :schedule_for => [3.hours.from_now],
93
- :device_tokens => ['DEVICE-TOKEN-THREE'],
94
- :aps => {:alert => 'You have a new message!', :badge => 1}
95
- }
96
- ]
97
-
98
- Urbanairship.batch_push(notifications)
99
- ```
100
-
101
- Sending broadcast notifications
102
- -------------------------------
103
- Urban Airship allows you to send a broadcast notification to all active registered device tokens for your app.
104
-
105
- ```ruby
106
- notification = {
107
- :schedule_for => [1.hour.from_now],
108
- :aps => {:alert => 'Important announcement!', :badge => 1}
109
- }
110
-
111
- Urbanairship.broadcast_push(notification)
112
- ```
113
-
114
- Polling the feedback API
115
- ------------------------
116
- The first time you attempt to send a push notification to a device that has uninstalled your app (or has opted-out of notifications), both Apple and Urban Airship will register that token in their feedback API. Urban Airship will prevent further attempted notification sends to that device, but it's a good practice to periodically poll Urban Airship's feedback API and mark those tokens as inactive in your own system as well.
117
-
118
- ```ruby
119
- # find all device tokens deactivated in the past 24 hours
120
- Urbanairship.feedback(24.hours.ago) # =>
121
- # [
122
- # {
123
- # "marked_inactive_on"=>"2011-06-03 22:53:23",
124
- # "alias"=>nil,
125
- # "device_token"=>"DEVICE-TOKEN-ONE"
126
- # },
127
- # {
128
- # "marked_inactive_on"=>"2011-06-03 22:53:23",
129
- # "alias"=>nil,
130
- # "device_token"=>"DEVICE-TOKEN-TWO"
131
- # }
132
- # ]
133
- ```
134
-
135
- Deleting scheduled notifications
136
- --------------------------------
137
-
138
- If you know the alias or id of a scheduled push notification then you can delete it from Urban Airship's queue and it will not be delivered.
139
-
140
- ```ruby
141
- Urbanairship.delete_scheduled_push("123456789")
142
- Urbanairship.delete_scheduled_push(123456789)
143
- Urbanairship.delete_scheduled_push(:alias => "deadbeef")
144
- ```
145
-
146
- Segments
147
- ---------------------------
148
- Urban Airship segments let you send a push notification to a subset of relevant users based on location, time, preferences, and behavior. You can read more about segments in the [Urban Airship docs](https://docs.urbanairship.com/display/DOCS/Server%3A+Segments+API).
149
-
150
- ```ruby
151
- notification = {
152
- :schedule_for => [1.hour.from_now],
153
- :segments => ['SEGMENT-ID'],
154
- :ios => {
155
- :aps => {
156
- :alert => 'You have a new message!', :badge => 1
157
- }
158
- },
159
- :android => {
160
- :alert => 'You have a new message!', :badge => 1
161
- }
162
- }
163
-
164
- Urbanairship.push_to_segment(notification)
165
- ```
166
-
167
- ### Creating a segment ###
168
- ``` ruby
169
- Urbanairship.create_segment({
170
- :display_name => 'segment1',
171
- :criteria => {:and => [{:tag => 'one'}, {:tag => 'two'}]}
172
- }) # => {}
173
- ```
174
-
175
- ### Listing your segments ###
176
-
177
- ```ruby
178
- Urbanairship.segments # =>
179
- # {
180
- # "segments" => [
181
- # {
182
- # "id" => "abcd-efgh-ijkl",
183
- # "display_name" => "segment1",
184
- # "creation_date" => 1360950614201,
185
- # "modification_date" => 1360950614201
186
- # }
187
- # ]
188
- # }
189
-
190
- Urbanairship.segment("abcd-efgh-ijkl") # =>
191
- # {
192
- # "id" => "abcd-efgh-ijkl",
193
- # "display_name" => "segment1",
194
- # "creation_date" => 1360950614201,
195
- # "modification_date" => 1360950614201
196
- # }
197
- ```
198
-
199
- ### Modifying a segment ###
200
- Note that you must provide both the display name and criteria when updating a segment, even if you are only changing one or the other.
201
- ``` ruby
202
- Urbanairship.update_segment('abcd-efgh-ijkl', {
203
- :display_name => 'segment1',
204
- :criteria => {:and => [{:tag => 'asdf'}]}
205
- }) # => {}
206
- ```
207
-
208
- ### Deleting a segment ###
209
- ```ruby
210
- Urbanairship.delete_segment("abcd-efgh-ijkl") # => {}
211
- ```
212
-
213
- Getting your device tokens
214
- -------------------------------------
215
- ```ruby
216
- Urbanairship.device_tokens # =>
217
- # {
218
- # "device_tokens" => {"device_token"=>"<token>", "active"=>true, "alias"=>"<alias>", "tags"=>[]},
219
- # "device_tokens_count" => 3,
220
- # "active_device_tokens_count" => 1
221
- # }
222
- ```
223
-
224
- Getting a count of your device tokens
225
- -------------------------------------
226
- ```ruby
227
- Urbanairship.device_tokens_count # =>
228
- # {
229
- # "device_tokens_count" => 3,
230
- # "active_device_tokens_count" => 1
231
- # }
232
- ```
233
-
234
- Tags
235
- ----
236
-
237
- Urban Airship allows you to create tags and associate them with devices. Then you can easily send a notification to every device matching a certain tag with a single call to the push API.
238
-
239
- ### Creating a tag ###
240
-
241
- Tags must be registered before you can use them.
242
-
243
- ```ruby
244
- Urbanairship.add_tag('TAG')
245
- ```
246
-
247
- ### Listing your tags ###
248
-
249
- ```ruby
250
- Urbanairship.tags
251
- ```
252
-
253
- ### Removing a tag ##
254
-
255
- This will remove a tag from your set of registered tags, as well as removing that tag from any devices that are currently using it.
256
-
257
- ```ruby
258
- Urbanairship.remove_tag('TAG')
259
- ```
260
-
261
- ### View tags associated with device ###
262
-
263
- ```ruby
264
- Urbanairship.tags_for_device('DEVICE-TOKEN')
265
- ```
266
-
267
- ### Tag a device ###
268
-
269
- ```ruby
270
- Urbanairship.tag_device(:device_token => 'DEVICE-TOKEN', :tag => 'TAG')
271
- ```
272
-
273
- You can also tag a device during device registration.
274
-
275
- ```ruby
276
- Urbanairship.register_device('DEVICE-TOKEN', :tags => ['san-francisco-users'])
277
- ```
278
-
279
- ### Untag a device ###
280
-
281
- ```ruby
282
- Urbanairship.untag_device(:device_token => 'DEVICE-TOKEN', :tag => 'TAG')
283
- ```
284
-
285
- ### Sending a notification to all devices with a given tag ###
286
-
287
- ```ruby
288
- notification = {
289
- :tags => ['san-francisco-users'],
290
- :aps => {:alert => 'Good morning San Francisco!', :badge => 1}
291
- }
292
-
293
- Urbanairship.push(notification)
294
- ```
295
-
296
- Using Urbanairship with Android
297
- -------------------------------
298
-
299
- The Urban Airship API extends a subset of their push API to Android devices. You can read more about what is currently supported [here](https://docs.urbanairship.com/display/DOCS/Server%3A+Android+Push+API), but as of this writing, only registration, aliases, tags, broadcast, individual push, and batch push are supported.
300
-
301
- To use this library with Android devices, you can set the `provider` configuration option to `:android`:
302
-
303
- ```ruby
304
- Urbanairship.provider = :android
305
- ```
306
-
307
- Alternatively, you can pass the `:provider => :android` option to device registration calls if your app uses Urbanairship to send notifications to both Android and iOS devices.
308
-
309
- ```ruby
310
- Urbanairship.register_device("DEVICE-TOKEN", :provider => :android)
311
- ```
312
-
313
- Note: all other supported actions use the same API endpoints as iOS, so it is not necessary to specify the provider as `:android` when calling them.
314
-
315
- When sending notifications to Android devices, the Urban Airship API expects the following basic structure:
316
-
317
- ```ruby
318
- notification = {
319
- :schedule_for => [1.hour.from_now],
320
- :apids => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
321
- :android => {:alert => 'You have a new message!', :extra => {:foo => 'bar'}}
322
- }
323
-
324
- Urbanairship.push(notification)
325
- ```
326
-
327
- -----------------------------
328
-
329
- Note: all public library methods will return either an array or a hash, depending on the response from the Urban Airship API. In addition, you can inspect these objects to find out if they were successful or not, and what the http response code from Urban Airship was.
330
-
331
- ```ruby
332
- response = Urbanairship.push(payload)
333
- response.success? # => true
334
- response.code # => '200'
335
- response.inspect # => "{\"scheduled_notifications\"=>[\"https://go.urbanairship.com/api/push/scheduled/123456\"]}"
336
- ```
337
-
338
- If the call to Urban Airship times out, you'll get a response object with a '503' code.
339
-
340
- ```ruby
341
- response = Urbanairship.feedback(1.year.ago)
342
- response.success? # => false
343
- response.code # => '503'
344
- response.inspect # => "{\"error\"=>\"Request timeout\"}"
345
- ```
346
-
347
- Instantiating an Urbanairship::Client
348
- -------------------------------------
349
-
350
- Anything you can do directly with the Urbanairship module, you can also do with a Client.
351
-
352
- ```ruby
353
- client = Urbanairship::Client.new
354
- client.application_key = 'application-key'
355
- client.application_secret = 'application-secret'
356
- client.register_device('DEVICE-TOKEN')
357
- notification = {
358
- :schedule_for => [1.hour.from_now],
359
- :device_tokens => ['DEVICE-TOKEN'],
360
- :aps => {:alert => 'You have a new message!', :badge => 1}
361
- }
362
-
363
- client.push(notification) # =>
364
- # {
365
- # "scheduled_notifications" => ["https://go.urbanairship.com/api/push/scheduled/123456"]
366
- # }
367
- ```
368
-
369
- This can be used to use clients for different Urbanairship applications in a thread-safe manner.
@@ -1,33 +0,0 @@
1
- module Urbanairship
2
- module Response
3
- module InstanceMethods
4
- attr_accessor :ua_response, :ua_options
5
-
6
- def code
7
- (ua_options[:code] || ua_response.code).to_s
8
- end
9
-
10
- def success?
11
- !!(code =~ /^2/)
12
- end
13
- end
14
-
15
- def self.wrap(response, options = {})
16
- if options[:body]
17
- output = options[:body]
18
- else
19
- begin
20
- output = JSON.parse(response.body || '{}')
21
- rescue JSON::ParserError => e
22
- output = {}
23
- end
24
- end
25
-
26
- output.extend(Urbanairship::Response::InstanceMethods)
27
- output.ua_response = response
28
- output.ua_options = options
29
-
30
- return output
31
- end
32
- end
33
- end
@@ -1,79 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Urbanairship::Response do
4
-
5
- before do
6
- FakeWeb.allow_net_connect = false
7
- end
8
-
9
- context "#code" do
10
- subject { Urbanairship.register_device("new_device_token") }
11
-
12
- before do
13
- FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/new_device_token", :status => ["201", "Created"])
14
- FakeWeb.register_uri(:put, /bad_key\:my_app_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"])
15
- Urbanairship.application_secret = "my_app_secret"
16
- end
17
-
18
- it "should be set correctly on new registraion token" do
19
- Urbanairship.application_key = "my_app_key"
20
- subject.code.should eql '201'
21
- end
22
-
23
- it "should set correctly on unauthhorized" do
24
- Urbanairship.application_key = "bad_key"
25
- subject.code.should eql '401'
26
- end
27
- end
28
-
29
- context "#success?" do
30
- subject { Urbanairship.register_device("new_device_token") }
31
-
32
- before do
33
- FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/new_device_token", :status => ["201", "Created"])
34
- FakeWeb.register_uri(:put, /bad_key\:my_app_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"])
35
- Urbanairship.application_secret = "my_app_secret"
36
- end
37
-
38
- it "should be true with good key" do
39
- Urbanairship.application_key = "my_app_key"
40
- subject.success?.should == true
41
- end
42
-
43
- it "should be false with bad key" do
44
- Urbanairship.application_key = "bad_key"
45
- subject.success?.should == false
46
- end
47
- end
48
-
49
- context "body array accessor" do
50
- subject { Urbanairship.feedback(Time.now) }
51
-
52
- before do
53
- FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "[{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"},{\"device_token\":\"token2\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}]")
54
- Urbanairship.application_secret = "my_app_secret"
55
- Urbanairship.application_key = "my_app_key"
56
- Urbanairship.master_secret = "my_master_secret"
57
- end
58
-
59
- it "should set up correct indexes" do
60
- subject[0]['device_token'].should eql "token"
61
- subject[1]['device_token'].should eql "token2"
62
- end
63
- end
64
-
65
- context "non array response" do
66
- subject { Urbanairship.feedback(Time.now) }
67
-
68
- before do
69
- FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}")
70
- Urbanairship.application_secret = "my_app_secret"
71
- Urbanairship.application_key = "my_app_key"
72
- Urbanairship.master_secret = "my_master_secret"
73
- end
74
-
75
- it "should set up correct keys" do
76
- subject['device_token'].should eql "token"
77
- end
78
- end
79
- end