fcm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/README.md +13 -9
  4. data/fcm.gemspec +1 -1
  5. data/lib/fcm.rb +55 -11
  6. data/spec/fcm_spec.rb +140 -3
  7. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 566f06986ceeeb5c1feb04bd49988c7d1a1698ed
4
- data.tar.gz: 60daa74b40a83b9536a54db75b04d8b02b1526f0
3
+ metadata.gz: 54f66db484403cdfb990353ebf8c52553de4beb9
4
+ data.tar.gz: 77bdb6784ce6688ee0f3cc468ecf3ab1e7ccd2b4
5
5
  SHA512:
6
- metadata.gz: 6c58de63c80e324d3425278ebebe22c617be80d2514a8d0a6944bda90a3c6bd06d2de3bb4afd137e1528fc363f97eb8e8671f3e0f7cf73a71381a0a75f60e107
7
- data.tar.gz: aaf37a387be61be0dbb3a094404204dfcb433549c68757dd4863ea97dbf80be236dcbd7706e298ac5148643af217aa76ad73a7e6eef14a06e009adbb650b2aba
6
+ metadata.gz: a97f8d1a9d24f436a928f288b302828022bb542529435a155d4758cd014ff19bfd5ce295e746597717d2b45e3e8d6523a97a1336e87555e734c492d31b6252bf
7
+ data.tar.gz: dc3ea1cf7d433759648a27e579000616f1fd322bf0d477dd5d67fc39ca7483dc2d12d870856585161d809df3d641c3482f0380af0168b69d1003312a71a30e83
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
- - 2.1.9
4
+ - 2.1.10
5
5
  - 2.2.5
6
6
  - 2.3.1
data/README.md CHANGED
@@ -57,16 +57,16 @@ Then you will need a notification key which you can create for a particular `key
57
57
  ```ruby
58
58
  response = fcm.create(key_name: "appUser-Chris",
59
59
  project_id: "my_project_id",
60
- registration_ids:["4", "8", "15", "16", "23", "42"])
60
+ registration_ids: ["4", "8", "15", "16", "23", "42"])
61
61
  ```
62
62
 
63
63
  ### Send to Notification Key
64
- Now you can send a message to a particular `notification_key` via the `send_with_notification_key` method. This allows the server to send a single data to multiple app instances (typically on multiple devices) owned by a single user (instead of sending to some registration tokens). Note: the maximum number of members allowed for a `notification_key` is 20.
64
+ Now you can send a message to a particular `notification_key` via the `send_with_notification_key` method. This allows the server to send a single [data](https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages) payload or/and [notification](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications) payload to multiple app instances (typically on multiple devices) owned by a single user (instead of sending to some registration tokens). Note: the maximum number of members allowed for a `notification_key` is 20.
65
65
 
66
66
  ```ruby
67
- response = fcm.send_with_notification_key("notification_key", {
67
+ response = fcm.send_with_notification_key("notification_key",
68
68
  data: {score: "3x1"},
69
- collapse_key: "updated_score"})
69
+ collapse_key: "updated_score")
70
70
  ```
71
71
 
72
72
  ### Add/Remove Registration Tokens
@@ -90,15 +90,15 @@ response = fcm.remove(key_name: "appUser-Chris",
90
90
  FCM [topic messaging](https://firebase.google.com/docs/cloud-messaging/topic-messaging) allows your app server to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports unlimited subscriptions per app. Sending to a topic is very similar to sending to an individual device or to a user group, in the sense that you can use the `fcm.send_with_notification_key()` method where the `noticiation_key` matches the regular expression `"/topics/[a-zA-Z0-9-_.~%]+"`:
91
91
 
92
92
  ```ruby
93
- response = fcm.send_with_notification_key("/topics/yourTopic", {
94
- data: {message: "This is a FCM Topic Message!"})
93
+ response = fcm.send_with_notification_key("/topics/yourTopic",
94
+ data: {message: "This is a FCM Topic Message!")
95
95
  ```
96
96
 
97
97
  Or you can use the helper:
98
98
 
99
99
  ```ruby
100
- response = fcm.send_to_topic("yourTopic", {
101
- data: {message: "This is a FCM Topic Message!"})
100
+ response = fcm.send_to_topic("yourTopic",
101
+ data: {message: "This is a FCM Topic Message!")
102
102
  ```
103
103
 
104
104
  ## Mobile Clients
@@ -109,11 +109,15 @@ The guide to set up an iOS app to get notifications is here: [Setting up a FCM C
109
109
 
110
110
  ## ChangeLog
111
111
 
112
+ ### 0.0.2
113
+
114
+ * Fixed group messaging url.
115
+ * Added API to `recover_notification_key`.
116
+
112
117
  ### 0.0.1
113
118
 
114
119
  * Initial version.
115
120
 
116
-
117
121
  ##MIT License
118
122
 
119
123
  * Copyright (c) 2016 Kashif Rasul and Shoaib Burq. See LICENSE.txt for details.
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fcm"
6
- s.version = "0.0.1"
6
+ s.version = "0.0.2"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Kashif Rasul", "Shoaib Burq"]
9
9
  s.email = ["kashif@spacialdb.com", "shoaib@spacialdb.com"]
data/lib/fcm.rb CHANGED
@@ -8,6 +8,9 @@ class FCM
8
8
  default_timeout 30
9
9
  format :json
10
10
 
11
+ # constants
12
+ GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com/gcm'
13
+
11
14
  attr_accessor :timeout, :api_key
12
15
 
13
16
  def initialize(api_key, client_options = {})
@@ -43,8 +46,8 @@ class FCM
43
46
  alias send send_notification
44
47
 
45
48
  def create_notification_key(key_name, project_id, registration_ids = [])
46
- post_body = build_post_body(registration_ids, operation: 'create',
47
- notification_key_name: key_name)
49
+ post_body = build_post_body(registration_ids, operation: 'create',
50
+ notification_key_name: key_name)
48
51
 
49
52
  params = {
50
53
  body: post_body.to_json,
@@ -55,15 +58,20 @@ class FCM
55
58
  }
56
59
  }
57
60
 
58
- response = self.class.post('/notification', params.merge(@client_options))
61
+ response = nil
62
+
63
+ for_uri(GROUP_NOTIFICATION_BASE_URI) do
64
+ response = self.class.post('/notification', params.merge(@client_options))
65
+ end
66
+
59
67
  build_response(response)
60
68
  end
61
69
  alias create create_notification_key
62
70
 
63
71
  def add_registration_ids(key_name, project_id, notification_key, registration_ids)
64
- post_body = build_post_body(registration_ids, operation: 'add',
65
- notification_key_name: key_name,
66
- notification_key: notification_key)
72
+ post_body = build_post_body(registration_ids, operation: 'add',
73
+ notification_key_name: key_name,
74
+ notification_key: notification_key)
67
75
 
68
76
  params = {
69
77
  body: post_body.to_json,
@@ -74,15 +82,19 @@ class FCM
74
82
  }
75
83
  }
76
84
 
77
- response = self.class.post('/notification', params.merge(@client_options))
85
+ response = nil
86
+
87
+ for_uri(GROUP_NOTIFICATION_BASE_URI) do
88
+ response = self.class.post('/notification', params.merge(@client_options))
89
+ end
78
90
  build_response(response)
79
91
  end
80
92
  alias add add_registration_ids
81
93
 
82
94
  def remove_registration_ids(key_name, project_id, notification_key, registration_ids)
83
- post_body = build_post_body(registration_ids, operation: 'remove',
84
- notification_key_name: key_name,
85
- notification_key: notification_key)
95
+ post_body = build_post_body(registration_ids, operation: 'remove',
96
+ notification_key_name: key_name,
97
+ notification_key: notification_key)
86
98
 
87
99
  params = {
88
100
  body: post_body.to_json,
@@ -93,11 +105,35 @@ class FCM
93
105
  }
94
106
  }
95
107
 
96
- response = self.class.post('/notification', params.merge(@client_options))
108
+ response = nil
109
+
110
+ for_uri(GROUP_NOTIFICATION_BASE_URI) do
111
+ response = self.class.post('/notification', params.merge(@client_options))
112
+ end
97
113
  build_response(response)
98
114
  end
99
115
  alias remove remove_registration_ids
100
116
 
117
+ def recover_notification_key(key_name, project_id)
118
+ params = {
119
+ query: {
120
+ notification_key_name: key_name
121
+ },
122
+ headers: {
123
+ 'Content-Type' => 'application/json',
124
+ 'project_id' => project_id,
125
+ 'Authorization' => "key=#{@api_key}"
126
+ }
127
+ }
128
+
129
+ response = nil
130
+
131
+ for_uri(GROUP_NOTIFICATION_BASE_URI) do
132
+ response = self.class.post('/notification', params.merge(@client_options))
133
+ end
134
+ build_response(response)
135
+ end
136
+
101
137
  def send_with_notification_key(notification_key, options = {})
102
138
  body = { to: notification_key }.merge(options)
103
139
 
@@ -108,6 +144,7 @@ class FCM
108
144
  'Content-Type' => 'application/json'
109
145
  }
110
146
  }
147
+
111
148
  response = self.class.post('/send', params.merge(@client_options))
112
149
  build_response(response)
113
150
  end
@@ -120,6 +157,13 @@ class FCM
120
157
 
121
158
  private
122
159
 
160
+ def for_uri(uri)
161
+ current_uri = self.class.base_uri
162
+ self.class.base_uri uri
163
+ yield
164
+ self.class.base_uri current_uri
165
+ end
166
+
123
167
  def build_post_body(registration_ids, options = {})
124
168
  { registration_ids: registration_ids }.merge(options)
125
169
  end
@@ -2,9 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe FCM do
4
4
  let(:send_url) { "#{FCM.base_uri}/send" }
5
+ let(:group_notification_base_uri) { "https://android.googleapis.com/gcm/notification" }
6
+ let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
7
+ let(:registration_ids) { ['42'] }
8
+ let(:key_name) { 'appUser-Chris' }
9
+ let(:project_id) { "123456789" } # https://developers.google.com/cloud-messaging/gcm#senderid
10
+ let(:notification_key) { "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ" }
5
11
 
6
12
  it 'should raise an error if the api key is not provided' do
7
- expect { FCM.new }.to raise_error
13
+ expect { FCM.new }.to raise_error(ArgumentError)
8
14
  end
9
15
 
10
16
  it 'should raise error if time_to_live is given' do
@@ -12,8 +18,6 @@ describe FCM do
12
18
  end
13
19
 
14
20
  describe 'sending notification' do
15
- let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
16
- let(:registration_ids) { ['42'] }
17
21
  let(:valid_request_body) do
18
22
  { registration_ids: registration_ids }
19
23
  end
@@ -238,4 +242,137 @@ describe FCM do
238
242
  end
239
243
  end
240
244
  end
245
+
246
+ describe 'sending group notifications' do
247
+ # TODO: refactor to should_behave_like
248
+ let(:valid_request_headers) do
249
+ {
250
+ "Authorization" => "key=#{api_key}",
251
+ "Content-Type" => 'application/json',
252
+ "Project-Id" => project_id
253
+ }
254
+ end
255
+ let(:valid_response_body) do
256
+ { notification_key: "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ" }
257
+ end
258
+
259
+ let(:default_valid_request_body) do
260
+ {
261
+ registration_ids: registration_ids,
262
+ operation: "create",
263
+ notification_key_name: key_name
264
+ }
265
+ end
266
+
267
+ subject { FCM.new(api_key) }
268
+
269
+ # ref: https://firebase.google.com/docs/cloud-messaging/notifications#managing-device-groups-on-the-app-server
270
+ context 'create' do
271
+ let(:valid_request_body) do
272
+ default_valid_request_body.merge({
273
+ operation: "create"
274
+ })
275
+ end
276
+
277
+ let(:mock_request_attributes) do
278
+ {
279
+ body: valid_request_body.to_json,
280
+ headers: valid_request_headers
281
+ }
282
+ end
283
+
284
+ before do
285
+ stub_request(:post, group_notification_base_uri).with(
286
+ mock_request_attributes
287
+ ).to_return(
288
+ body: valid_response_body.to_json,
289
+ headers: {},
290
+ status: 200
291
+ )
292
+ end
293
+
294
+ it 'should send a post request' do
295
+ response = subject.create(key_name, project_id, registration_ids)
296
+ response.should eq(
297
+ headers: {},
298
+ status_code: 200,
299
+ response: 'success',
300
+ body: valid_response_body.to_json
301
+ )
302
+ end
303
+ end # create context
304
+
305
+ context 'add' do
306
+ let(:valid_request_body) do
307
+ default_valid_request_body.merge({
308
+ operation: "add",
309
+ notification_key: notification_key
310
+ })
311
+ end
312
+
313
+ let(:mock_request_attributes) do
314
+ {
315
+ body: valid_request_body.to_json,
316
+ headers: valid_request_headers
317
+ }
318
+ end
319
+
320
+ before do
321
+ stub_request(:post, group_notification_base_uri).with(
322
+ mock_request_attributes
323
+ ).to_return(
324
+ body: valid_response_body.to_json,
325
+ headers: {},
326
+ status: 200
327
+ )
328
+ end
329
+
330
+ it 'should send a post request' do
331
+ response = subject.add(key_name, project_id, notification_key, registration_ids)
332
+ response.should eq(
333
+ headers: {},
334
+ status_code: 200,
335
+ response: 'success',
336
+ body: valid_response_body.to_json
337
+ )
338
+ end
339
+ end # add context
340
+
341
+ context 'remove' do
342
+ let(:valid_request_body) do
343
+ default_valid_request_body.merge({
344
+ operation: "remove",
345
+ notification_key: notification_key
346
+ })
347
+ end
348
+
349
+ let(:mock_request_attributes) do
350
+ {
351
+ body: valid_request_body.to_json,
352
+ headers: valid_request_headers
353
+ }
354
+ end
355
+
356
+ before do
357
+ stub_request(:post, group_notification_base_uri).with(
358
+ mock_request_attributes
359
+ ).to_return(
360
+ body: valid_response_body.to_json,
361
+ headers: {},
362
+ status: 200
363
+ )
364
+ end
365
+
366
+ it 'should send a post request' do
367
+ response = subject.remove(key_name, project_id, notification_key, registration_ids)
368
+ response.should eq(
369
+ headers: {},
370
+ status_code: 200,
371
+ response: 'success',
372
+ body: valid_response_body.to_json
373
+ )
374
+ end
375
+ end # remove context
376
+
377
+ end
241
378
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kashif Rasul
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-19 00:00:00.000000000 Z
12
+ date: 2016-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project: fcm
83
- rubygems_version: 2.6.4
83
+ rubygems_version: 2.6.6
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: Reliably deliver messages and notifications via FCM