gcm 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/LICENSE.txt +1 -1
- data/README.md +45 -21
- data/gcm.gemspec +1 -1
- data/lib/gcm.rb +53 -50
- data/spec/gcm_spec.rb +98 -112
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a6bbfaf64cd67f8f2008a650c0321ca5667717d
|
4
|
+
data.tar.gz: 4ba52e54a1239330ee62cc69acebca3efad1ff03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d2418f9c98cb8f43194dddaba988cb61761d2142f19c05e8718780628ef07df519f3d19523212e987b7ce2ee86ff1b63fbeede50c364379b2c7090d75ce74d9
|
7
|
+
data.tar.gz: 5b2e606c93a2f79dc8c11fa4e0d442399ad23d6616740ef862ca8ad8840e04dc248f55fc0812558301ffd2ce7ac1f1d25b667e46e57a53d6417fe2caccf32d4c
|
data/.travis.yml
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Google Cloud Messaging for Android (GCM)
|
2
|
-
[](http://travis-ci.org/spacialdb/gcm)
|
2
|
+
[](http://badge.fury.io/rb/gcm) [](http://travis-ci.org/spacialdb/gcm)
|
3
3
|
|
4
|
-
GCM sends notifications to Android devices via [GCM](
|
4
|
+
GCM sends notifications to Android devices via [GCM](https://developers.google.com/cloud-messaging/gcm).
|
5
5
|
|
6
6
|
##Installation
|
7
7
|
|
@@ -15,18 +15,18 @@ gem 'gcm'
|
|
15
15
|
|
16
16
|
##Requirements
|
17
17
|
|
18
|
-
An Android device running 2.3 or newer and an API key as per [GCM getting started guide](
|
18
|
+
An Android device running 2.3 (or newer) or an iOS device and an API key as per [GCM getting started guide](https://developers.google.com/cloud-messaging/android/start).
|
19
19
|
|
20
20
|
One of the following, tested Ruby versions:
|
21
21
|
|
22
|
-
* `1.9.3`
|
23
22
|
* `2.0.0`
|
24
|
-
* `2.1.
|
25
|
-
* `2.2.
|
23
|
+
* `2.1.8`
|
24
|
+
* `2.2.4`
|
25
|
+
* `2.3.0`
|
26
26
|
|
27
27
|
##Usage
|
28
28
|
|
29
|
-
For your server to send a message to one or more devices, you must first initialise a new `GCM` class with your
|
29
|
+
For your server to send a message to one or more devices, you must first initialise a new `GCM` class with your Api key, and then call the `send` method on this and give it 1 or more (up to 1000) registration tokens as an array of strings. You can also optionally send further [HTTP message parameters](https://developers.google.com/cloud-messaging/http-server-ref) like `data` or `time_to_live` etc. as a hash via the second optional argument to `send`.
|
30
30
|
|
31
31
|
Example sending notifications:
|
32
32
|
|
@@ -36,31 +36,31 @@ require 'gcm'
|
|
36
36
|
gcm = GCM.new("my_api_key")
|
37
37
|
# you can set option parameters in here
|
38
38
|
# - all options are pass to HTTParty method arguments
|
39
|
-
# - ref: https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb#
|
39
|
+
# - ref: https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb#L29-L60
|
40
40
|
# gcm = GCM.new("my_api_key", timeout: 3)
|
41
41
|
|
42
|
-
registration_ids= ["12", "13"] # an array of one or more client registration
|
42
|
+
registration_ids= ["12", "13"] # an array of one or more client registration tokens
|
43
43
|
options = {data: {score: "123"}, collapse_key: "updated_score"}
|
44
44
|
response = gcm.send(registration_ids, options)
|
45
45
|
```
|
46
46
|
|
47
|
-
Currently `response` is just a hash containing the response `body`, `headers` and `status`. Check [here](
|
47
|
+
Currently `response` is just a hash containing the response `body`, `headers` and `status`. Check [here](https://developers.google.com/cloud-messaging/http#response) to see how to interpret the responses.
|
48
48
|
|
49
|
-
##
|
49
|
+
## Device Group Messaging
|
50
50
|
|
51
|
-
With [
|
51
|
+
With [device group messaging](https://developers.google.com/cloud-messaging/notifications), you can send a single message to multiple instance of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user. However, a group could also represent a set of devices where the app instance functions in a highly correlated manner. To use this feature, you will first need an initialised `GCM` class.
|
52
52
|
|
53
|
-
### Generate a Notification Key
|
54
|
-
Then you will need a notification key which you can create for a particular `key_name` which needs to be uniquely named per app in case you have multiple apps for the same `project_id`. This ensures that notifications only go to the intended target app. The `create` method will do this and return the token `notification_key
|
53
|
+
### Generate a Notification Key for device group
|
54
|
+
Then you will need a notification key which you can create for a particular `key_name` which needs to be uniquely named per app in case you have multiple apps for the same `project_id`. This ensures that notifications only go to the intended target app. The `create` method will do this and return the token `notification_key`, that represents the device group, in the response:
|
55
55
|
|
56
56
|
```ruby
|
57
57
|
response = gcm.create(key_name: "appUser-Chris",
|
58
|
-
project_id: "my_project_id",
|
58
|
+
project_id: "my_project_id", # https://developers.google.com/cloud-messaging/gcm#senderid
|
59
59
|
registration_ids:["4", "8", "15", "16", "23", "42"])
|
60
60
|
```
|
61
61
|
|
62
62
|
### Send to Notification Key
|
63
|
-
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 registration
|
63
|
+
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
64
|
|
65
65
|
```ruby
|
66
66
|
response = gcm.send_with_notification_key("notification_key", {
|
@@ -68,9 +68,9 @@ response = gcm.send_with_notification_key("notification_key", {
|
|
68
68
|
collapse_key: "updated_score"})
|
69
69
|
```
|
70
70
|
|
71
|
-
### Add/Remove Registration
|
71
|
+
### Add/Remove Registration Tokens
|
72
72
|
|
73
|
-
You can also add/remove registration
|
73
|
+
You can also add/remove registration Tokens to/from a particular `notification_key` of some `project_id`. For example:
|
74
74
|
|
75
75
|
```ruby
|
76
76
|
response = gcm.add(key_name: "appUser-Chris",
|
@@ -84,6 +84,22 @@ response = gcm.remove(key_name: "appUser-Chris",
|
|
84
84
|
registration_ids:["8", "15"])
|
85
85
|
```
|
86
86
|
|
87
|
+
## Send Messages to Topics
|
88
|
+
|
89
|
+
GCM 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 `gcm.send_with_notification_key()` method where the `noticiation_key` matches the regular expression `"/topics/[a-zA-Z0-9-_.~%]+"`:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
response = gcm.send_with_notification_key("/topics/yourTopic", {
|
93
|
+
data: {message: "This is a GCM Topic Message!"})
|
94
|
+
```
|
95
|
+
|
96
|
+
Or you can use the helper:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
response = gcm.send_to_topic("yourTopic", {
|
100
|
+
data: {message: "This is a GCM Topic Message!"})
|
101
|
+
```
|
102
|
+
|
87
103
|
## Blog Posts
|
88
104
|
|
89
105
|
* [How to send iOS and Android notifications from your Rails backend](http://juretriglav.si/how-to-send-ios-and-android-notifications-from-your-rails-backend/)
|
@@ -91,13 +107,21 @@ response = gcm.remove(key_name: "appUser-Chris",
|
|
91
107
|
* [GCM – 서버 만들기](http://susemi99.kr/1023)
|
92
108
|
* [ruby から gcm を使って android 端末へメッセージを送信する](http://qiita.com/ma2saka/items/5852308b7c2855eef552)
|
93
109
|
* [titanium alloy android push通知 by ruby](http://shoprev.hatenablog.com/entry/2014/08/30/202531)
|
110
|
+
* [Android Push Notifications via Rails](http://azukiweb.com/blog/2015/android-push-nots/)
|
94
111
|
|
95
|
-
##
|
112
|
+
## Mobile Clients
|
96
113
|
|
97
|
-
You can find a guide to implement an Android Client app to receive notifications here: [
|
114
|
+
You can find a guide to implement an Android Client app to receive notifications here: [Set up a GCM Client App on Android](https://developers.google.com/cloud-messaging/android/client).
|
115
|
+
|
116
|
+
The guide to set up an iOS app to get notifications is here: [Setting up a GCM Client App on iOS](https://developers.google.com/cloud-messaging/ios/client).
|
98
117
|
|
99
118
|
## ChangeLog
|
100
119
|
|
120
|
+
### 0.1.1
|
121
|
+
|
122
|
+
* Added helper `send_to_topic` to send messages to [topics](https://developers.google.com/cloud-messaging/topic-messaging).
|
123
|
+
* Fixed documentation and updated base uri to `https://gcm-http.googleapis.com/gcm`
|
124
|
+
|
101
125
|
### 0.1.0
|
102
126
|
* Added `send_with_notification_key` to send message to a notification key since the documented API for it is [wrong]( http://stackoverflow.com/questions/19720767/gcm-user-notifications-missing-registration-ids-field/25183892#25183892).
|
103
127
|
|
@@ -121,7 +145,7 @@ You can find a guide to implement an Android Client app to receive notifications
|
|
121
145
|
|
122
146
|
##MIT License
|
123
147
|
|
124
|
-
* Copyright (c)
|
148
|
+
* Copyright (c) 2016 Kashif Rasul and Shoaib Burq. See LICENSE.txt for details.
|
125
149
|
|
126
150
|
##Many thanks to all the contributors
|
127
151
|
|
data/gcm.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "gcm"
|
6
|
-
s.version = "0.1.
|
6
|
+
s.version = "0.1.1"
|
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/gcm.rb
CHANGED
@@ -4,7 +4,7 @@ require 'json'
|
|
4
4
|
|
5
5
|
class GCM
|
6
6
|
include HTTParty
|
7
|
-
base_uri 'https://
|
7
|
+
base_uri 'https://gcm-http.googleapis.com/gcm'
|
8
8
|
default_timeout 30
|
9
9
|
format :json
|
10
10
|
|
@@ -31,25 +31,24 @@ class GCM
|
|
31
31
|
post_body = build_post_body(registration_ids, options)
|
32
32
|
|
33
33
|
params = {
|
34
|
-
:
|
35
|
-
:
|
34
|
+
body: post_body.to_json,
|
35
|
+
headers: {
|
36
36
|
'Authorization' => "key=#{@api_key}",
|
37
|
-
'Content-Type' => 'application/json'
|
37
|
+
'Content-Type' => 'application/json'
|
38
38
|
}
|
39
39
|
}
|
40
40
|
response = self.class.post('/send', params.merge(@client_options))
|
41
41
|
build_response(response, registration_ids)
|
42
42
|
end
|
43
|
-
|
43
|
+
alias send send_notification
|
44
44
|
|
45
|
-
def create_notification_key(key_name, project_id, registration_ids=[])
|
46
|
-
post_body = build_post_body(registration_ids,
|
47
|
-
|
48
|
-
:notification_key_name => key_name})
|
45
|
+
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
48
|
|
50
49
|
params = {
|
51
|
-
:
|
52
|
-
:
|
50
|
+
body: post_body.to_json,
|
51
|
+
headers: {
|
53
52
|
'Content-Type' => 'application/json',
|
54
53
|
'project_id' => project_id,
|
55
54
|
'Authorization' => "key=#{@api_key}"
|
@@ -59,17 +58,16 @@ class GCM
|
|
59
58
|
response = self.class.post('/notification', params.merge(@client_options))
|
60
59
|
build_response(response)
|
61
60
|
end
|
62
|
-
|
61
|
+
alias create create_notification_key
|
63
62
|
|
64
63
|
def add_registration_ids(key_name, project_id, notification_key, registration_ids)
|
65
|
-
post_body = build_post_body(registration_ids,
|
66
|
-
|
67
|
-
|
68
|
-
:notification_key => notification_key})
|
64
|
+
post_body = build_post_body(registration_ids, operation: 'add',
|
65
|
+
notification_key_name: key_name,
|
66
|
+
notification_key: notification_key)
|
69
67
|
|
70
68
|
params = {
|
71
|
-
:
|
72
|
-
:
|
69
|
+
body: post_body.to_json,
|
70
|
+
headers: {
|
73
71
|
'Content-Type' => 'application/json',
|
74
72
|
'project_id' => project_id,
|
75
73
|
'Authorization' => "key=#{@api_key}"
|
@@ -79,17 +77,16 @@ class GCM
|
|
79
77
|
response = self.class.post('/notification', params.merge(@client_options))
|
80
78
|
build_response(response)
|
81
79
|
end
|
82
|
-
|
80
|
+
alias add add_registration_ids
|
83
81
|
|
84
82
|
def remove_registration_ids(key_name, project_id, notification_key, registration_ids)
|
85
|
-
post_body = build_post_body(registration_ids,
|
86
|
-
|
87
|
-
|
88
|
-
:notification_key => notification_key})
|
83
|
+
post_body = build_post_body(registration_ids, operation: 'remove',
|
84
|
+
notification_key_name: key_name,
|
85
|
+
notification_key: notification_key)
|
89
86
|
|
90
87
|
params = {
|
91
|
-
:
|
92
|
-
:
|
88
|
+
body: post_body.to_json,
|
89
|
+
headers: {
|
93
90
|
'Content-Type' => 'application/json',
|
94
91
|
'project_id' => project_id,
|
95
92
|
'Authorization' => "key=#{@api_key}"
|
@@ -99,45 +96,51 @@ class GCM
|
|
99
96
|
response = self.class.post('/notification', params.merge(@client_options))
|
100
97
|
build_response(response)
|
101
98
|
end
|
102
|
-
|
99
|
+
alias remove remove_registration_ids
|
103
100
|
|
104
|
-
def send_with_notification_key(notification_key, options)
|
105
|
-
body = { :
|
101
|
+
def send_with_notification_key(notification_key, options = {})
|
102
|
+
body = { to: notification_key }.merge(options)
|
106
103
|
|
107
104
|
params = {
|
108
|
-
:
|
109
|
-
:
|
105
|
+
body: body.to_json,
|
106
|
+
headers: {
|
110
107
|
'Authorization' => "key=#{@api_key}",
|
111
|
-
'Content-Type' => 'application/json'
|
108
|
+
'Content-Type' => 'application/json'
|
112
109
|
}
|
113
110
|
}
|
114
111
|
response = self.class.post('/send', params.merge(@client_options))
|
115
112
|
build_response(response)
|
116
113
|
end
|
117
114
|
|
115
|
+
def send_to_topic(topic, options = {})
|
116
|
+
if topic =~ /[a-zA-Z0-9\-_.~%]+/
|
117
|
+
send_with_notification_key('/topics/' + topic, options)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
118
121
|
private
|
119
122
|
|
120
|
-
def build_post_body(registration_ids, options={})
|
121
|
-
{ :
|
123
|
+
def build_post_body(registration_ids, options = {})
|
124
|
+
{ registration_ids: registration_ids }.merge(options)
|
122
125
|
end
|
123
126
|
|
124
|
-
def build_response(response, registration_ids=[])
|
127
|
+
def build_response(response, registration_ids = [])
|
125
128
|
body = response.body || {}
|
126
|
-
response_hash = {:
|
129
|
+
response_hash = { body: body, headers: response.headers, status_code: response.code }
|
127
130
|
case response.code
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
131
|
+
when 200
|
132
|
+
response_hash[:response] = 'success'
|
133
|
+
body = JSON.parse(body) unless body.empty?
|
134
|
+
response_hash[:canonical_ids] = build_canonical_ids(body, registration_ids) unless registration_ids.empty?
|
135
|
+
response_hash[:not_registered_ids] = build_not_registered_ids(body, registration_ids) unless registration_ids.empty?
|
136
|
+
when 400
|
137
|
+
response_hash[:response] = 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.'
|
138
|
+
when 401
|
139
|
+
response_hash[:response] = 'There was an error authenticating the sender account.'
|
140
|
+
when 503
|
141
|
+
response_hash[:response] = 'Server is temporarily unavailable.'
|
142
|
+
when 500..599
|
143
|
+
response_hash[:response] = 'There was an internal error in the GCM server while trying to process the request.'
|
141
144
|
end
|
142
145
|
response_hash
|
143
146
|
end
|
@@ -147,7 +150,7 @@ class GCM
|
|
147
150
|
unless body.empty?
|
148
151
|
if body['canonical_ids'] > 0
|
149
152
|
body['results'].each_with_index do |result, index|
|
150
|
-
canonical_ids << { :
|
153
|
+
canonical_ids << { old: registration_ids[index], new: result['registration_id'] } if has_canonical_id?(result)
|
151
154
|
end
|
152
155
|
end
|
153
156
|
end
|
@@ -158,7 +161,7 @@ class GCM
|
|
158
161
|
not_registered_ids = []
|
159
162
|
unless body.empty?
|
160
163
|
if body['failure'] > 0
|
161
|
-
body['results'].each_with_index do |result,index|
|
164
|
+
body['results'].each_with_index do |result, index|
|
162
165
|
not_registered_ids << registration_id[index] if is_not_registered?(result)
|
163
166
|
end
|
164
167
|
end
|
data/spec/gcm_spec.rb
CHANGED
@@ -1,38 +1,38 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GCM do
|
4
|
-
let(:send_url) { "#{GCM
|
4
|
+
let(:send_url) { "#{GCM.base_uri}/send" }
|
5
5
|
|
6
|
-
it
|
7
|
-
expect {GCM.new}.to raise_error
|
6
|
+
it 'should raise an error if the api key is not provided' do
|
7
|
+
expect { GCM.new }.to raise_error
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'should raise error if time_to_live is given' do
|
11
11
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#send-msg
|
12
12
|
end
|
13
13
|
|
14
|
-
describe
|
14
|
+
describe 'sending notification' do
|
15
15
|
let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
|
16
|
-
let(:registration_ids) { [
|
16
|
+
let(:registration_ids) { ['42'] }
|
17
17
|
let(:valid_request_body) do
|
18
|
-
{ :
|
18
|
+
{ registration_ids: registration_ids }
|
19
19
|
end
|
20
20
|
let(:valid_request_headers) do
|
21
21
|
{
|
22
|
-
|
23
|
-
|
22
|
+
'Content-Type' => 'application/json',
|
23
|
+
'Authorization' => "key=#{api_key}"
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
27
27
|
let(:stub_gcm_send_request) do
|
28
28
|
stub_request(:post, send_url).with(
|
29
|
-
:
|
30
|
-
:
|
29
|
+
body: valid_request_body.to_json,
|
30
|
+
headers: valid_request_headers
|
31
31
|
).to_return(
|
32
32
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
33
|
-
:
|
34
|
-
:
|
35
|
-
:
|
33
|
+
body: '{}',
|
34
|
+
headers: {},
|
35
|
+
status: 200
|
36
36
|
)
|
37
37
|
end
|
38
38
|
|
@@ -40,7 +40,7 @@ describe GCM do
|
|
40
40
|
uri = URI.parse(send_url)
|
41
41
|
uri.user = 'a'
|
42
42
|
uri.password = 'b'
|
43
|
-
stub_request(:post, uri.to_s).to_return(:
|
43
|
+
stub_request(:post, uri.to_s).to_return(body: '{}', headers: {}, status: 200)
|
44
44
|
end
|
45
45
|
|
46
46
|
before(:each) do
|
@@ -48,212 +48,198 @@ describe GCM do
|
|
48
48
|
stub_gcm_send_request_with_basic_auth
|
49
49
|
end
|
50
50
|
|
51
|
-
it
|
51
|
+
it 'should send notification using POST to GCM server' do
|
52
52
|
gcm = GCM.new(api_key)
|
53
|
-
gcm.send(registration_ids).should eq(
|
53
|
+
gcm.send(registration_ids).should eq(response: 'success', body: '{}', headers: {}, status_code: 200, canonical_ids: [], not_registered_ids: [])
|
54
54
|
stub_gcm_send_request.should have_been_made.times(1)
|
55
55
|
end
|
56
56
|
|
57
|
-
it
|
58
|
-
gcm = GCM.new(api_key, basic_auth: {username: 'a', password: 'b'})
|
59
|
-
gcm.send(registration_ids).should eq(
|
57
|
+
it 'should use basic authentication provided by options' do
|
58
|
+
gcm = GCM.new(api_key, basic_auth: { username: 'a', password: 'b' })
|
59
|
+
gcm.send(registration_ids).should eq(response: 'success', body: '{}', headers: {}, status_code: 200, canonical_ids: [], not_registered_ids: [])
|
60
60
|
stub_gcm_send_request_with_basic_auth.should have_been_made.times(1)
|
61
61
|
end
|
62
62
|
|
63
|
-
context
|
64
|
-
let!(:stub_with_data)
|
65
|
-
stub_request(:post, send_url)
|
66
|
-
with(:
|
67
|
-
|
68
|
-
to_return(:
|
69
|
-
|
63
|
+
context 'send notification with data' do
|
64
|
+
let!(:stub_with_data) do
|
65
|
+
stub_request(:post, send_url)
|
66
|
+
.with(body: '{"registration_ids":["42"],"data":{"score":"5x1","time":"15:10"}}',
|
67
|
+
headers: valid_request_headers)
|
68
|
+
.to_return(status: 200, body: '', headers: {})
|
69
|
+
end
|
70
70
|
before do
|
71
71
|
end
|
72
|
-
it
|
72
|
+
it 'should send the data in a post request to gcm' do
|
73
73
|
gcm = GCM.new(api_key)
|
74
|
-
gcm.send(registration_ids,
|
74
|
+
gcm.send(registration_ids, data: { score: '5x1', time: '15:10' })
|
75
75
|
stub_with_data.should have_been_requested
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
context
|
80
|
-
|
79
|
+
context 'when send_notification responds with failure' do
|
81
80
|
let(:mock_request_attributes) do
|
82
81
|
{
|
83
|
-
:
|
84
|
-
:
|
82
|
+
body: valid_request_body.to_json,
|
83
|
+
headers: valid_request_headers
|
85
84
|
}
|
86
85
|
end
|
87
86
|
|
88
87
|
subject { GCM.new(api_key) }
|
89
88
|
|
90
|
-
context
|
89
|
+
context 'on failure code 400' do
|
91
90
|
before do
|
92
91
|
stub_request(:post, send_url).with(
|
93
92
|
mock_request_attributes
|
94
93
|
).to_return(
|
95
94
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
96
|
-
:
|
97
|
-
:
|
98
|
-
:
|
95
|
+
body: '{}',
|
96
|
+
headers: {},
|
97
|
+
status: 400
|
99
98
|
)
|
100
99
|
end
|
101
|
-
it
|
102
|
-
subject.send(registration_ids).should eq({
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
:status_code => 400
|
107
|
-
})
|
100
|
+
it 'should not send notification due to 400' do
|
101
|
+
subject.send(registration_ids).should eq(body: '{}',
|
102
|
+
headers: {},
|
103
|
+
response: 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.',
|
104
|
+
status_code: 400)
|
108
105
|
end
|
109
106
|
end
|
110
107
|
|
111
|
-
context
|
108
|
+
context 'on failure code 401' do
|
112
109
|
before do
|
113
110
|
stub_request(:post, send_url).with(
|
114
111
|
mock_request_attributes
|
115
112
|
).to_return(
|
116
113
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
117
|
-
:
|
118
|
-
:
|
119
|
-
:
|
114
|
+
body: '{}',
|
115
|
+
headers: {},
|
116
|
+
status: 401
|
120
117
|
)
|
121
118
|
end
|
122
119
|
|
123
|
-
it
|
124
|
-
subject.send(registration_ids).should eq({
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
:status_code => 401
|
129
|
-
})
|
120
|
+
it 'should not send notification due to 401' do
|
121
|
+
subject.send(registration_ids).should eq(body: '{}',
|
122
|
+
headers: {},
|
123
|
+
response: 'There was an error authenticating the sender account.',
|
124
|
+
status_code: 401)
|
130
125
|
end
|
131
126
|
end
|
132
127
|
|
133
|
-
context
|
128
|
+
context 'on failure code 503' do
|
134
129
|
before do
|
135
130
|
stub_request(:post, send_url).with(
|
136
131
|
mock_request_attributes
|
137
132
|
).to_return(
|
138
133
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
139
|
-
:
|
140
|
-
:
|
141
|
-
:
|
134
|
+
body: '{}',
|
135
|
+
headers: {},
|
136
|
+
status: 503
|
142
137
|
)
|
143
138
|
end
|
144
139
|
|
145
|
-
it
|
146
|
-
subject.send(registration_ids).should eq({
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
:status_code => 503
|
151
|
-
})
|
140
|
+
it 'should not send notification due to 503' do
|
141
|
+
subject.send(registration_ids).should eq(body: '{}',
|
142
|
+
headers: {},
|
143
|
+
response: 'Server is temporarily unavailable.',
|
144
|
+
status_code: 503)
|
152
145
|
end
|
153
146
|
end
|
154
147
|
|
155
|
-
context
|
148
|
+
context 'on failure code 5xx' do
|
156
149
|
before do
|
157
150
|
stub_request(:post, send_url).with(
|
158
151
|
mock_request_attributes
|
159
152
|
).to_return(
|
160
153
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
161
|
-
:
|
162
|
-
:
|
163
|
-
:
|
154
|
+
body: '{"body-key" => "Body value"}',
|
155
|
+
headers: { 'header-key' => 'Header value' },
|
156
|
+
status: 599
|
164
157
|
)
|
165
158
|
end
|
166
159
|
|
167
|
-
it
|
168
|
-
subject.send(registration_ids).should eq({
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
:status_code => 599
|
173
|
-
})
|
160
|
+
it 'should not send notification due to 599' do
|
161
|
+
subject.send(registration_ids).should eq(body: '{"body-key" => "Body value"}',
|
162
|
+
headers: { 'header-key' => ['Header value'] },
|
163
|
+
response: 'There was an internal error in the GCM server while trying to process the request.',
|
164
|
+
status_code: 599)
|
174
165
|
end
|
175
166
|
end
|
176
167
|
end
|
177
168
|
|
178
|
-
context
|
179
|
-
|
169
|
+
context 'when send_notification responds canonical_ids' do
|
180
170
|
let(:mock_request_attributes) do
|
181
171
|
{
|
182
|
-
|
183
|
-
|
172
|
+
body: valid_request_body.to_json,
|
173
|
+
headers: valid_request_headers
|
184
174
|
}
|
185
175
|
end
|
186
176
|
|
187
177
|
let(:valid_response_body_with_canonical_ids) do
|
188
178
|
{
|
189
|
-
:
|
179
|
+
failure: 0, canonical_ids: 1, results: [{ registration_id: '43', message_id: '0:1385025861956342%572c22801bb3' }]
|
190
180
|
}
|
191
181
|
end
|
192
182
|
|
193
183
|
subject { GCM.new(api_key) }
|
194
184
|
|
195
|
-
|
196
185
|
before do
|
197
186
|
stub_request(:post, send_url).with(
|
198
|
-
|
187
|
+
mock_request_attributes
|
199
188
|
).to_return(
|
200
189
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
201
|
-
:
|
202
|
-
:
|
203
|
-
:
|
190
|
+
body: valid_response_body_with_canonical_ids.to_json,
|
191
|
+
headers: {},
|
192
|
+
status: 200
|
204
193
|
)
|
205
194
|
end
|
206
195
|
|
207
|
-
it
|
196
|
+
it 'should contain canonical_ids' do
|
208
197
|
response = subject.send(registration_ids)
|
209
198
|
|
210
|
-
response.should eq({
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
:body => "{\"failure\":0,\"canonical_ids\":1,\"results\":[{\"registration_id\":\"43\",\"message_id\":\"0:1385025861956342%572c22801bb3\"}]}"
|
217
|
-
})
|
199
|
+
response.should eq(headers: {},
|
200
|
+
canonical_ids: [{ old: '42', new: '43' }],
|
201
|
+
not_registered_ids: [],
|
202
|
+
status_code: 200,
|
203
|
+
response: 'success',
|
204
|
+
body: '{"failure":0,"canonical_ids":1,"results":[{"registration_id":"43","message_id":"0:1385025861956342%572c22801bb3"}]}')
|
218
205
|
end
|
219
206
|
end
|
220
207
|
|
221
|
-
context
|
222
|
-
|
208
|
+
context 'when send_notification responds with NotRegistered' do
|
223
209
|
subject { GCM.new(api_key) }
|
224
210
|
|
225
211
|
let(:mock_request_attributes) do
|
226
212
|
{
|
227
|
-
|
228
|
-
|
213
|
+
body: valid_request_body.to_json,
|
214
|
+
headers: valid_request_headers
|
229
215
|
}
|
230
216
|
end
|
231
217
|
|
232
218
|
let(:valid_response_body_with_not_registered_ids) do
|
233
219
|
{
|
234
|
-
:
|
220
|
+
canonical_ids: 0, failure: 1, results: [{ error: 'NotRegistered' }]
|
235
221
|
}
|
236
222
|
end
|
237
223
|
|
238
224
|
before do
|
239
|
-
stub_request(:post,send_url).with(
|
225
|
+
stub_request(:post, send_url).with(
|
240
226
|
mock_request_attributes
|
241
227
|
).to_return(
|
242
|
-
:
|
243
|
-
:
|
244
|
-
:
|
228
|
+
body: valid_response_body_with_not_registered_ids.to_json,
|
229
|
+
headers: {},
|
230
|
+
status: 200
|
245
231
|
)
|
246
232
|
end
|
247
233
|
|
248
|
-
it
|
234
|
+
it 'should contain not_registered_ids' do
|
249
235
|
response = subject.send(registration_ids)
|
250
236
|
response.should eq(
|
251
|
-
:
|
252
|
-
:
|
253
|
-
:
|
254
|
-
:
|
255
|
-
:
|
256
|
-
:
|
237
|
+
headers: {},
|
238
|
+
canonical_ids: [],
|
239
|
+
not_registered_ids: registration_ids,
|
240
|
+
status_code: 200,
|
241
|
+
response: 'success',
|
242
|
+
body: '{"canonical_ids":0,"failure":1,"results":[{"error":"NotRegistered"}]}'
|
257
243
|
)
|
258
244
|
end
|
259
245
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gcm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
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:
|
12
|
+
date: 2016-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
version: '0'
|
80
80
|
requirements: []
|
81
81
|
rubyforge_project: gcm
|
82
|
-
rubygems_version: 2.
|
82
|
+
rubygems_version: 2.5.2
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: send data to Android applications on Android devices
|