gcm 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile +2 -2
- data/README.md +57 -7
- data/gcm.gemspec +1 -1
- data/lib/gcm.rb +66 -7
- data/spec/gcm_spec.rb +24 -22
- 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: 5b27baf730ba346c79357e983c7c44c03527300c
|
4
|
+
data.tar.gz: 4a2387383fd90c378bfb58ec669484b3255edfa0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ee69d0a009f6f99bc9cce3e7fc5f1462c709d1d10d53069d04ff7ff8abe618de869772f37a76614f532bf1e78f67b3ac84bd430aacf99e8436078af11f974cc
|
7
|
+
data.tar.gz: d1e2b6606961975e16aef602a58f006ae6cb5d17300ecc03168eae6966e11416599a816aa7b858499daf0724c4f16926a20a1c8674923e5159de4e779f5b36eb
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -21,42 +21,92 @@ One of the following, tested Ruby versions:
|
|
21
21
|
|
22
22
|
* `1.9.3`
|
23
23
|
* `2.0.0`
|
24
|
-
* `2.1.
|
24
|
+
* `2.1.2`
|
25
25
|
|
26
26
|
##Usage
|
27
27
|
|
28
|
-
For your server to send a message to one or more devices, you must first
|
28
|
+
For your server to send a message to one or more devices, you must first initialise a new `GCM` class with your [api key](https://developer.android.com/google/gcm/gs.html#access-key), and then call the `send` method on this and give it 1 or more (up to 1000) registration IDs as an array of strings. You can also optionally send further [HTTP message parameters](http://developer.android.com/google/gcm/server.html#params) like `data` or `time_to_live` etc. as a hash via the second optional argument to `send`.
|
29
29
|
|
30
30
|
Example sending notifications:
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
require 'gcm'
|
34
34
|
|
35
|
-
gcm = GCM.new(
|
35
|
+
gcm = GCM.new("my_api_key")
|
36
36
|
# you can set option parameters in here
|
37
37
|
# - all options are pass to HTTParty method arguments
|
38
38
|
# - ref: https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb#L40-L68
|
39
|
-
# gcm = GCM.new(
|
39
|
+
# gcm = GCM.new("my_api_key", timeout: 3)
|
40
40
|
|
41
41
|
registration_ids= ["12", "13"] # an array of one or more client registration IDs
|
42
42
|
options = {data: {score: "123"}, collapse_key: "updated_score"}
|
43
|
-
response = gcm.
|
43
|
+
response = gcm.send(registration_ids, options)
|
44
44
|
```
|
45
45
|
|
46
46
|
Currently `response` is just a hash containing the response `body`, `headers` and `status`. Check [here](http://developer.android.com/google/gcm/http.html#response) to see how to interpret the responses.
|
47
47
|
|
48
|
+
## User Notifications
|
49
|
+
|
50
|
+
With [user notifications](http://developer.android.com/google/gcm/notifications.html), you can send a single message to multiple instance of an app running on devices owned by a single user. To use this feature, you will first need an initialised `GCM` class.
|
51
|
+
|
52
|
+
### Generate a Notification Key
|
53
|
+
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` in the response:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
response = gcm.create(key_name: "appUser-Chris",
|
57
|
+
project_id: "my_project_id",
|
58
|
+
registration_ids:["4", "8", "15", "16", "23", "42"])
|
59
|
+
```
|
60
|
+
|
61
|
+
### Send to Notification Key
|
62
|
+
Now you can send a message to a particular `notification_key` via the `send` 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 IDs). Note: the maximum number of members allowed for a `notification_key` is 10.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
response = gcm.send([], {
|
66
|
+
notification_key: "appUser-Chris-key",
|
67
|
+
data: {score: "3x1"},
|
68
|
+
collapse_key: "updated_score"})
|
69
|
+
```
|
70
|
+
|
71
|
+
### Add/Remove Registration IDs
|
72
|
+
|
73
|
+
You can also add/remove registration IDs to/from a particular `notification_key` of some `project_id`. For example:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
response = gcm.add(key_name: "appUser-Chris",
|
77
|
+
project_id: "my_project_id",
|
78
|
+
notification_key:"appUser-Chris-key"
|
79
|
+
registration_ids:["7", "3"])
|
80
|
+
|
81
|
+
response = gcm.remove(key_name: "appUser-Chris",
|
82
|
+
project_id: "my_project_id",
|
83
|
+
notification_key:"appUser-Chris-key"
|
84
|
+
registration_ids:["8", "15"])
|
85
|
+
```
|
86
|
+
|
48
87
|
## Blog posts
|
49
88
|
|
50
|
-
* [How to send iOS and Android notifications from your Rails backend](http://
|
89
|
+
* [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/)
|
90
|
+
* [Как отправлять push уведомления из Вашего Rails приложения](http://habrahabr.ru/post/214607/)
|
91
|
+
* [GCM – 서버 만들기](http://susemi99.kr/1023)
|
92
|
+
* [ruby から gcm を使って android 端末へメッセージを送信する](http://qiita.com/ma2saka/items/5852308b7c2855eef552)
|
93
|
+
|
94
|
+
## Android Client
|
95
|
+
|
96
|
+
You can find an Android Client app to receive notifications from here: [Google Cloud Message - Client Android](https://github.com/mikebolivar/gcm)
|
51
97
|
|
52
98
|
## ChangeLog
|
53
99
|
|
100
|
+
## 0.0.8
|
101
|
+
* Added support for User Notifications API
|
102
|
+
* Added alias method `send` for `send_notification`
|
103
|
+
|
54
104
|
## 0.0.7
|
55
105
|
* All responses now have a body and header hashes
|
56
106
|
|
57
107
|
### 0.0.6
|
58
108
|
|
59
|
-
* You can
|
109
|
+
* You can initialise GCM class with [HTTParty Options](https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb#L41-L69)
|
60
110
|
|
61
111
|
### 0.0.5
|
62
112
|
|
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.0.
|
6
|
+
s.version = "0.0.8"
|
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,8 +4,7 @@ require 'json'
|
|
4
4
|
|
5
5
|
class GCM
|
6
6
|
include HTTParty
|
7
|
-
|
8
|
-
base_uri PUSH_URL
|
7
|
+
base_uri 'https://android.googleapis.com/gcm'
|
9
8
|
default_timeout 30
|
10
9
|
format :json
|
11
10
|
|
@@ -26,8 +25,8 @@ class GCM
|
|
26
25
|
# "time": "15:10"
|
27
26
|
# }
|
28
27
|
# }
|
29
|
-
# gcm = GCM.new(
|
30
|
-
# gcm.
|
28
|
+
# gcm = GCM.new("API_KEY")
|
29
|
+
# gcm.send(registration_ids: ["4sdsx", "8sdsd"], {data: {score: "5x1"}})
|
31
30
|
def send_notification(registration_ids, options = {})
|
32
31
|
post_body = build_post_body(registration_ids, options)
|
33
32
|
|
@@ -38,9 +37,69 @@ class GCM
|
|
38
37
|
'Content-Type' => 'application/json',
|
39
38
|
}
|
40
39
|
}
|
41
|
-
response = self.class.post('', params.merge(@client_options))
|
40
|
+
response = self.class.post('/send', params.merge(@client_options))
|
42
41
|
build_response(response, registration_ids)
|
43
42
|
end
|
43
|
+
alias_method :send, :send_notification
|
44
|
+
|
45
|
+
def create_notification_key(key_name, project_id, registration_ids=[])
|
46
|
+
post_body = build_post_body(registration_ids, {
|
47
|
+
:operation => "create",
|
48
|
+
:notification_key_name => key_name})
|
49
|
+
|
50
|
+
params = {
|
51
|
+
:body => post_body.to_json,
|
52
|
+
:headers => {
|
53
|
+
'Content-Type' => 'application/json',
|
54
|
+
'project_id' => project_id,
|
55
|
+
'Authorization' => "key=#{@api_key}"
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
response = self.class.post('/notification', params.merge(@client_options))
|
60
|
+
build_response(response)
|
61
|
+
end
|
62
|
+
alias_method :create, :create_notification_key
|
63
|
+
|
64
|
+
def add_registration_ids(key_name, project_id, notification_key, registration_ids)
|
65
|
+
post_body = build_post_body(registration_ids, {
|
66
|
+
:operation => "add",
|
67
|
+
:notification_key_name => key_name,
|
68
|
+
:notification_key => notification_key})
|
69
|
+
|
70
|
+
params = {
|
71
|
+
:body => post_body.to_json,
|
72
|
+
:headers => {
|
73
|
+
'Content-Type' => 'application/json',
|
74
|
+
'project_id' => project_id,
|
75
|
+
'Authorization' => "key=#{@api_key}"
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
response = self.class.post('/notification', params.merge(@client_options))
|
80
|
+
build_response(response)
|
81
|
+
end
|
82
|
+
alias_method :add, :add_registration_ids
|
83
|
+
|
84
|
+
def remove_registration_ids(key_name, project_id, notification_key, registration_ids)
|
85
|
+
post_body = build_post_body(registration_ids, {
|
86
|
+
:operation => "remove",
|
87
|
+
:notification_key_name => key_name,
|
88
|
+
:notification_key => notification_key})
|
89
|
+
|
90
|
+
params = {
|
91
|
+
:body => post_body.to_json,
|
92
|
+
:headers => {
|
93
|
+
'Content-Type' => 'application/json',
|
94
|
+
'project_id' => project_id,
|
95
|
+
'Authorization' => "key=#{@api_key}"
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
response = self.class.post('/notification', params.merge(@client_options))
|
100
|
+
build_response(response)
|
101
|
+
end
|
102
|
+
alias_method :remove, :remove_registration_ids
|
44
103
|
|
45
104
|
private
|
46
105
|
|
@@ -48,13 +107,13 @@ class GCM
|
|
48
107
|
{ :registration_ids => registration_ids }.merge(options)
|
49
108
|
end
|
50
109
|
|
51
|
-
def build_response(response, registration_ids)
|
110
|
+
def build_response(response, registration_ids=[])
|
52
111
|
body = response.body || {}
|
53
112
|
response_hash = {:body => body, :headers => response.headers, :status_code => response.code}
|
54
113
|
case response.code
|
55
114
|
when 200
|
56
115
|
response_hash[:response] = 'success'
|
57
|
-
response_hash[:canonical_ids] = build_canonical_ids(body, registration_ids)
|
116
|
+
response_hash[:canonical_ids] = build_canonical_ids(body, registration_ids) unless registration_ids.empty?
|
58
117
|
when 400
|
59
118
|
response_hash[:response] = 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.'
|
60
119
|
when 401
|
data/spec/gcm_spec.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe GCM do
|
4
|
+
let(:send_url) { "#{GCM::base_uri}/send" }
|
5
|
+
|
4
6
|
it "should raise an error if the api key is not provided" do
|
5
7
|
expect {GCM.new}.to raise_error
|
6
8
|
end
|
@@ -22,8 +24,8 @@ describe GCM do
|
|
22
24
|
}
|
23
25
|
end
|
24
26
|
|
25
|
-
let(:
|
26
|
-
stub_request(:post,
|
27
|
+
let(:stub_gcm_send_request) do
|
28
|
+
stub_request(:post, send_url).with(
|
27
29
|
:body => valid_request_body.to_json,
|
28
30
|
:headers => valid_request_headers
|
29
31
|
).to_return(
|
@@ -34,33 +36,33 @@ describe GCM do
|
|
34
36
|
)
|
35
37
|
end
|
36
38
|
|
37
|
-
let(:
|
38
|
-
uri = URI.parse(
|
39
|
+
let(:stub_gcm_send_request_with_basic_auth) do
|
40
|
+
uri = URI.parse(send_url)
|
39
41
|
uri.user = 'a'
|
40
42
|
uri.password = 'b'
|
41
43
|
stub_request(:post, uri.to_s).to_return(:body => {}, :headers => {}, :status => 200)
|
42
44
|
end
|
43
45
|
|
44
46
|
before(:each) do
|
45
|
-
|
46
|
-
|
47
|
+
stub_gcm_send_request
|
48
|
+
stub_gcm_send_request_with_basic_auth
|
47
49
|
end
|
48
50
|
|
49
51
|
it "should send notification using POST to GCM server" do
|
50
52
|
gcm = GCM.new(api_key)
|
51
|
-
gcm.
|
52
|
-
|
53
|
+
gcm.send(registration_ids).should eq({:response => 'success', :body => {}, :headers => {}, :status_code => 200, :canonical_ids => []})
|
54
|
+
stub_gcm_send_request.should have_been_made.times(1)
|
53
55
|
end
|
54
56
|
|
55
57
|
it "should use basic authentication provided by options" do
|
56
58
|
gcm = GCM.new(api_key, basic_auth: {username: 'a', password: 'b'})
|
57
|
-
gcm.
|
58
|
-
|
59
|
+
gcm.send(registration_ids).should eq({:response => 'success', :body => {}, :headers => {}, :status_code => 200, :canonical_ids => []})
|
60
|
+
stub_gcm_send_request_with_basic_auth.should have_been_made.times(1)
|
59
61
|
end
|
60
62
|
|
61
63
|
context "send notification with data" do
|
62
64
|
let!(:stub_with_data){
|
63
|
-
stub_request(:post,
|
65
|
+
stub_request(:post, send_url).
|
64
66
|
with(:body => "{\"registration_ids\":[\"42\"],\"data\":{\"score\":\"5x1\",\"time\":\"15:10\"}}",
|
65
67
|
:headers => valid_request_headers ).
|
66
68
|
to_return(:status => 200, :body => "", :headers => {})
|
@@ -69,7 +71,7 @@ describe GCM do
|
|
69
71
|
end
|
70
72
|
it "should send the data in a post request to gcm" do
|
71
73
|
gcm = GCM.new(api_key)
|
72
|
-
gcm.
|
74
|
+
gcm.send(registration_ids, { :data => { :score => "5x1", :time => "15:10"} })
|
73
75
|
stub_with_data.should have_been_requested
|
74
76
|
end
|
75
77
|
end
|
@@ -87,7 +89,7 @@ describe GCM do
|
|
87
89
|
|
88
90
|
context "on failure code 400" do
|
89
91
|
before do
|
90
|
-
stub_request(:post,
|
92
|
+
stub_request(:post, send_url).with(
|
91
93
|
mock_request_attributes
|
92
94
|
).to_return(
|
93
95
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
@@ -97,7 +99,7 @@ describe GCM do
|
|
97
99
|
)
|
98
100
|
end
|
99
101
|
it "should not send notification due to 400" do
|
100
|
-
subject.
|
102
|
+
subject.send(registration_ids).should eq({
|
101
103
|
:body => {},
|
102
104
|
:headers => {},
|
103
105
|
:response => "Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.",
|
@@ -108,7 +110,7 @@ describe GCM do
|
|
108
110
|
|
109
111
|
context "on failure code 401" do
|
110
112
|
before do
|
111
|
-
stub_request(:post,
|
113
|
+
stub_request(:post, send_url).with(
|
112
114
|
mock_request_attributes
|
113
115
|
).to_return(
|
114
116
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
@@ -119,7 +121,7 @@ describe GCM do
|
|
119
121
|
end
|
120
122
|
|
121
123
|
it "should not send notification due to 401" do
|
122
|
-
subject.
|
124
|
+
subject.send(registration_ids).should eq({
|
123
125
|
:body => {},
|
124
126
|
:headers => {},
|
125
127
|
:response => "There was an error authenticating the sender account.",
|
@@ -130,7 +132,7 @@ describe GCM do
|
|
130
132
|
|
131
133
|
context "on failure code 503" do
|
132
134
|
before do
|
133
|
-
stub_request(:post,
|
135
|
+
stub_request(:post, send_url).with(
|
134
136
|
mock_request_attributes
|
135
137
|
).to_return(
|
136
138
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
@@ -141,7 +143,7 @@ describe GCM do
|
|
141
143
|
end
|
142
144
|
|
143
145
|
it "should not send notification due to 503" do
|
144
|
-
subject.
|
146
|
+
subject.send(registration_ids).should eq({
|
145
147
|
:body => {},
|
146
148
|
:headers => {},
|
147
149
|
:response => 'Server is temporarily unavailable.',
|
@@ -152,7 +154,7 @@ describe GCM do
|
|
152
154
|
|
153
155
|
context "on failure code 5xx" do
|
154
156
|
before do
|
155
|
-
stub_request(:post,
|
157
|
+
stub_request(:post, send_url).with(
|
156
158
|
mock_request_attributes
|
157
159
|
).to_return(
|
158
160
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
@@ -163,7 +165,7 @@ describe GCM do
|
|
163
165
|
end
|
164
166
|
|
165
167
|
it "should not send notification due to 599" do
|
166
|
-
subject.
|
168
|
+
subject.send(registration_ids).should eq({
|
167
169
|
:body => { "body-key" => "Body value" },
|
168
170
|
:headers => { "header-key" => ["Header value"] },
|
169
171
|
:response => 'There was an internal error in the GCM server while trying to process the request.',
|
@@ -192,7 +194,7 @@ describe GCM do
|
|
192
194
|
|
193
195
|
|
194
196
|
before do
|
195
|
-
stub_request(:post,
|
197
|
+
stub_request(:post, send_url).with(
|
196
198
|
mock_request_attributes
|
197
199
|
).to_return(
|
198
200
|
# ref: http://developer.android.com/guide/google/gcm/gcm.html#success
|
@@ -203,7 +205,7 @@ describe GCM do
|
|
203
205
|
end
|
204
206
|
|
205
207
|
it "should contain canonical_ids" do
|
206
|
-
response = subject.
|
208
|
+
response = subject.send(registration_ids)
|
207
209
|
|
208
210
|
response.should eq({
|
209
211
|
:headers => {},
|
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.0.
|
4
|
+
version: 0.0.8
|
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: 2014-
|
12
|
+
date: 2014-08-11 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.2.
|
82
|
+
rubygems_version: 2.2.2
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: send data to Android applications on Android devices
|