chaione-pushmeup 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rvmrc +48 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/Keychain Access.jpg +0 -0
- data/LICENSE +22 -0
- data/README.md +208 -0
- data/Rakefile +13 -0
- data/lib/pushmeup.rb +3 -0
- data/lib/pushmeup/android.rb +2 -0
- data/lib/pushmeup/apns/core.rb +91 -0
- data/lib/pushmeup/apns/notification.rb +39 -0
- data/lib/pushmeup/apple.rb +2 -0
- data/lib/pushmeup/gcm/core.rb +114 -0
- data/lib/pushmeup/gcm/notification.rb +46 -0
- data/lib/pushmeup/version.rb +3 -0
- data/pushmeup.gemspec +36 -0
- data/spec/lib/pushmeup_spec.rb +57 -0
- data/spec/spec_helper.rb +1 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86b9ecf271051c27fdfe5f514fcbf370bca76b59
|
4
|
+
data.tar.gz: d1a63a9fcdb717da29412b4060f8376210094cc6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 658c6a36c84b746bc455d3c8dbe7620301850df6f38fce465a480e99d65c6d3f2d27e8e2f9baf6a76d661f05ac715070d3e03f7643d932016a3f3886366863d8
|
7
|
+
data.tar.gz: d384e32aab801adab313a75f4eb1c68e14cfe7ab0dc31946d1922b492eccd66720fbce4441144cbe954da2f14f8f33defc871f2e113f7d5c4b461f3aeeb910c2
|
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3@pushmeup"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.11.5 (master)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
if [[ -s Gemfile ]] && {
|
38
|
+
! builtin command -v bundle >/dev/null ||
|
39
|
+
builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
}
|
41
|
+
then
|
42
|
+
printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
gem install bundler
|
44
|
+
fi
|
45
|
+
if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
then
|
47
|
+
bundle install | grep -vE '^Using|Your bundle is complete'
|
48
|
+
fi
|
data/.travis.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.2
|
5
|
+
- 1.9.3
|
6
|
+
- jruby-18mode # JRuby in 1.8 mode
|
7
|
+
- jruby-19mode # JRuby in 1.9 mode
|
8
|
+
- rbx-18mode
|
9
|
+
- rbx-19mode
|
10
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
11
|
+
# script: bundle exec rspec spec
|
data/Gemfile
ADDED
data/Keychain Access.jpg
ADDED
Binary file
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Nicos Karalis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,208 @@
|
|
1
|
+
# Pushmeup
|
2
|
+
|
3
|
+
### a gem for various push notification services.
|
4
|
+
|
5
|
+
## Goals
|
6
|
+
|
7
|
+
Pushmeup is an attempt to create an push notifications center that could send push to devices like:
|
8
|
+
|
9
|
+
- Android
|
10
|
+
- iOS
|
11
|
+
- Mac OS X
|
12
|
+
- Windows Phone
|
13
|
+
- And many others
|
14
|
+
|
15
|
+
Currently we have only support for ``iOS`` and ``Android`` but we are planning code for more plataforms.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
$ gem install pushmeup
|
20
|
+
|
21
|
+
or add to your ``Gemfile``
|
22
|
+
|
23
|
+
gem 'pushmeup'
|
24
|
+
|
25
|
+
and install it with
|
26
|
+
|
27
|
+
$ bundle install
|
28
|
+
|
29
|
+
## APNS (Apple iOS)
|
30
|
+
|
31
|
+
### Configure
|
32
|
+
|
33
|
+
1. In Keychain access export your certificate and your private key as a ``p12``.
|
34
|
+
|
35
|
+
![Keychain Access](https://raw.github.com/NicosKaralis/pushmeup/master/Keychain Access.jpg)
|
36
|
+
|
37
|
+
2. Run the following command to convert the ``p12`` to a ``pem`` file
|
38
|
+
|
39
|
+
$ openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
|
40
|
+
|
41
|
+
3. After you have created your ``pem`` file. Set the host, port and certificate file location on the APNS class. You just need to set this once:
|
42
|
+
|
43
|
+
APNS.host = 'gateway.push.apple.com'
|
44
|
+
# gateway.sandbox.push.apple.com is default
|
45
|
+
|
46
|
+
APNS.port = 2195
|
47
|
+
# this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
|
48
|
+
|
49
|
+
APNS.pem = '/path/to/pem/file'
|
50
|
+
# this is the file you just created
|
51
|
+
|
52
|
+
APNS.pass = ''
|
53
|
+
# Just in case your pem need a password
|
54
|
+
|
55
|
+
Alternatively, If you don't have the certificate stored in a file, you can pass any object that responds to ``read``.
|
56
|
+
|
57
|
+
APNS.pem = StringIO.new(pem_string)
|
58
|
+
|
59
|
+
### Usage
|
60
|
+
|
61
|
+
#### Sending a single notification:
|
62
|
+
|
63
|
+
device_token = '123abc456def'
|
64
|
+
APNS.send_notification(device_token, 'Hello iPhone!' )
|
65
|
+
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
|
66
|
+
|
67
|
+
#### Sending multiple notifications
|
68
|
+
|
69
|
+
device_token = '123abc456def'
|
70
|
+
n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )
|
71
|
+
n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
|
72
|
+
APNS.send_notifications([n1, n2])
|
73
|
+
|
74
|
+
#### Sending more information along
|
75
|
+
|
76
|
+
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
|
77
|
+
:other => {:sent => 'with apns gem', :custom_param => "value"})
|
78
|
+
|
79
|
+
this will result in a payload like this:
|
80
|
+
|
81
|
+
{"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem", "custom_param":"value"}
|
82
|
+
|
83
|
+
### Getting your iOS device token
|
84
|
+
|
85
|
+
- (void)applicationDidFinishLaunching:(UIApplication *)application {
|
86
|
+
// Register with apple that this app will use push notification
|
87
|
+
...
|
88
|
+
|
89
|
+
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
|
90
|
+
|
91
|
+
...
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
96
|
+
// Show the device token obtained from apple to the log
|
97
|
+
NSLog("deviceToken: %", deviceToken);
|
98
|
+
}
|
99
|
+
|
100
|
+
## GCM (Google Cloud Messaging)
|
101
|
+
|
102
|
+
### Configure
|
103
|
+
|
104
|
+
GCM.host = 'https://android.googleapis.com/gcm/send'
|
105
|
+
# https://android.googleapis.com/gcm/send is default
|
106
|
+
|
107
|
+
GCM.format = :json
|
108
|
+
# :json is default and only available at the moment
|
109
|
+
|
110
|
+
GCM.key = "123abc456def"
|
111
|
+
# this is the apiKey obtained from here https://code.google.com/apis/console/
|
112
|
+
|
113
|
+
### Usage
|
114
|
+
|
115
|
+
#### Sending a single notification:
|
116
|
+
|
117
|
+
destination = ["device1", "device2", "device3"]
|
118
|
+
# can be an string or an array of strings containing the regIds of the devices you want to send
|
119
|
+
|
120
|
+
data = {:key => "value", :key2 => ["array", "value"]}
|
121
|
+
# must be an hash with all values you want inside you notification
|
122
|
+
|
123
|
+
GCM.send_notification( destination )
|
124
|
+
# Empty notification
|
125
|
+
|
126
|
+
GCM.send_notification( destination, data )
|
127
|
+
# Notification with custom information
|
128
|
+
|
129
|
+
GCM.send_notification( destination, data, :collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false )
|
130
|
+
# Notification with custom information and parameters
|
131
|
+
|
132
|
+
for more information on parameters check documentation: [GCM | Android Developers](http://developer.android.com/guide/google/gcm/gcm.html#request)
|
133
|
+
|
134
|
+
#### Sending multiple notifications:
|
135
|
+
|
136
|
+
destination1 = "device1"
|
137
|
+
destination2 = ["device2"]
|
138
|
+
destination3 = ["device1", "device2", "device3"]
|
139
|
+
# can be an string or an array of strings containing the regIds of the devices you want to send
|
140
|
+
|
141
|
+
data1 = {:key => "value", :key2 => ["array", "value"]}
|
142
|
+
# must be an hash with all values you want inside you notification
|
143
|
+
|
144
|
+
options1 = {:collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false}
|
145
|
+
# options for the notification
|
146
|
+
|
147
|
+
n1 = GCM::Notification.new(destination1, data1, options1)
|
148
|
+
n2 = GCM::Notification.new(destination2, data2)
|
149
|
+
n3 = GCM::Notification.new(destination3, data3, options2)
|
150
|
+
|
151
|
+
GCM.send_notifications( [n1, n2, n3] )
|
152
|
+
# In this case, every notification has his own parameters
|
153
|
+
|
154
|
+
for more information on parameters check documentation: [GCM | Android Developers](http://developer.android.com/guide/google/gcm/gcm.html#request)
|
155
|
+
|
156
|
+
#### Getting your Android device token (regId)
|
157
|
+
|
158
|
+
Check this link [GCM: Getting Started](http://developer.android.com/guide/google/gcm/gs.html)
|
159
|
+
|
160
|
+
### (Optional) You can add multiple keys for GCM
|
161
|
+
|
162
|
+
You can use multiple keys to send notifications, to do it just do this changes in the code
|
163
|
+
|
164
|
+
#### Configure
|
165
|
+
|
166
|
+
GCM.key = { :key1 => "123abc456def", :key2 => "456def123abc" }
|
167
|
+
# the ``:key1`` and the ``:key2`` can be any object, they can be the projectID, the date, the version, doesn't matter.
|
168
|
+
# The only restrain is: they need to be valid keys for a hash.
|
169
|
+
|
170
|
+
#### Usage
|
171
|
+
|
172
|
+
# For single notification
|
173
|
+
GCM.send_notification( destination, :identity => :key1 )
|
174
|
+
# Empty notification
|
175
|
+
|
176
|
+
GCM.send_notification( destination, data, :identity => :key1 )
|
177
|
+
# Notification with custom information
|
178
|
+
|
179
|
+
GCM.send_notification( destination, data, :collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false, :identity => :key1 )
|
180
|
+
# Notification with custom information and parameters
|
181
|
+
|
182
|
+
# For multiple notifications
|
183
|
+
options1 = {}
|
184
|
+
options2 = {..., :identity => :key2}
|
185
|
+
n1 = GCM::Notification.new(destination1, data1, options1.merge({:identity => :key2}))
|
186
|
+
n2 = GCM::Notification.new(destination2, data2, :identity => :key1)
|
187
|
+
n3 = GCM::Notification.new(destination3, data3, options2)
|
188
|
+
|
189
|
+
GCM.send_notifications( [n1, n2, n3] )
|
190
|
+
# In this case, every notification has his own parameters, options and key
|
191
|
+
|
192
|
+
## Status
|
193
|
+
|
194
|
+
#### Build Status [![Build Status](https://secure.travis-ci.org/NicosKaralis/pushmeup.png?branch=master)](http://travis-ci.org/NicosKaralis/pushmeup) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/NicosKaralis/pushmeup)
|
195
|
+
|
196
|
+
#### Dependency Status [![Dependency Status](https://gemnasium.com/NicosKaralis/pushmeup.png?travis)](https://gemnasium.com/NicosKaralis/pushmeup)
|
197
|
+
|
198
|
+
## Contributing
|
199
|
+
|
200
|
+
We would be very pleased if you want to help us!
|
201
|
+
|
202
|
+
Currently we need a lot of testing so if you are good at writing tests please help us
|
203
|
+
|
204
|
+
## License
|
205
|
+
|
206
|
+
Pushmeup is released under the MIT license:
|
207
|
+
|
208
|
+
http://www.opensource.org/licenses/MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new
|
6
|
+
|
7
|
+
task :default => :spec
|
8
|
+
task :test => :spec
|
9
|
+
|
10
|
+
desc "Open an irb session preloaded with this library"
|
11
|
+
task :console do
|
12
|
+
sh "irb -rubygems -I lib -r pushmeup.rb"
|
13
|
+
end
|
data/lib/pushmeup.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'openssl'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module APNS
|
6
|
+
|
7
|
+
@host = 'gateway.sandbox.push.apple.com'
|
8
|
+
@port = 2195
|
9
|
+
# openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
|
10
|
+
@pem = nil # this should be the path of the pem file not the contentes
|
11
|
+
@pass = nil
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :host, :pem, :port, :pass
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.send_notification(device_token, message)
|
18
|
+
n = APNS::Notification.new(device_token, message)
|
19
|
+
self.send_notifications([n])
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.send_notifications(notifications)
|
23
|
+
sock, ssl = self.open_connection
|
24
|
+
|
25
|
+
notifications.each do |n|
|
26
|
+
ssl.write(n.packaged_notification)
|
27
|
+
end
|
28
|
+
|
29
|
+
ssl.close
|
30
|
+
sock.close
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.feedback
|
34
|
+
sock, ssl = self.feedback_connection
|
35
|
+
|
36
|
+
apns_feedback = []
|
37
|
+
|
38
|
+
while line = ssl.read(38) # Read lines from the socket
|
39
|
+
line.strip!
|
40
|
+
f = line.unpack('N1n1H140')
|
41
|
+
apns_feedback << { :timestamp => Time.at(f[0]), :token => f[2] }
|
42
|
+
end
|
43
|
+
|
44
|
+
ssl.close
|
45
|
+
sock.close
|
46
|
+
|
47
|
+
return apns_feedback
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def self.pem_data
|
53
|
+
raise "Your pem file is not set. (APNS.pem = /path/to/cert.pem or object that responds to read)" unless pem
|
54
|
+
if pem.respond_to? :read
|
55
|
+
data = pem.read
|
56
|
+
pem.rewind if pem.respond_to(:rewind)
|
57
|
+
else
|
58
|
+
raise "The path to your pem file does not exist!" unless File.exist?(pem)
|
59
|
+
data = File.read(pem)
|
60
|
+
end
|
61
|
+
data
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.open_connection
|
65
|
+
context = OpenSSL::SSL::SSLContext.new
|
66
|
+
context.cert = OpenSSL::X509::Certificate.new(self.pem_data)
|
67
|
+
context.key = OpenSSL::PKey::RSA.new(self.pem_data, self.pass)
|
68
|
+
|
69
|
+
sock = TCPSocket.new(self.host, self.port)
|
70
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
|
71
|
+
ssl.connect
|
72
|
+
|
73
|
+
return sock, ssl
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.feedback_connection
|
77
|
+
context = OpenSSL::SSL::SSLContext.new
|
78
|
+
context.cert = OpenSSL::X509::Certificate.new(self.pem_data)
|
79
|
+
context.key = OpenSSL::PKey::RSA.new(self.pem_data, self.pass)
|
80
|
+
|
81
|
+
fhost = self.host.gsub('gateway','feedback')
|
82
|
+
puts fhost
|
83
|
+
|
84
|
+
sock = TCPSocket.new(fhost, 2196)
|
85
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock, context)
|
86
|
+
ssl.connect
|
87
|
+
|
88
|
+
return sock, ssl
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module APNS
|
2
|
+
class Notification
|
3
|
+
attr_accessor :device_token, :alert, :badge, :sound, :other
|
4
|
+
|
5
|
+
def initialize(device_token, message)
|
6
|
+
self.device_token = device_token
|
7
|
+
if message.is_a?(Hash)
|
8
|
+
self.alert = message[:alert]
|
9
|
+
self.badge = message[:badge]
|
10
|
+
self.sound = message[:sound]
|
11
|
+
self.other = message[:other]
|
12
|
+
elsif message.is_a?(String)
|
13
|
+
self.alert = message
|
14
|
+
else
|
15
|
+
raise "Notification needs to have either a hash or string"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def packaged_notification
|
20
|
+
pt = self.packaged_token
|
21
|
+
pm = self.packaged_message
|
22
|
+
[0, 0, 32, pt, 0, pm.bytesize, pm].pack("ccca*cca*")
|
23
|
+
end
|
24
|
+
|
25
|
+
def packaged_token
|
26
|
+
[device_token.gsub(/[\s|<|>]/,'')].pack('H*')
|
27
|
+
end
|
28
|
+
|
29
|
+
def packaged_message
|
30
|
+
aps = {'aps'=> {} }
|
31
|
+
aps['aps']['alert'] = self.alert if self.alert
|
32
|
+
aps['aps']['badge'] = self.badge if self.badge
|
33
|
+
aps['aps']['sound'] = self.sound if self.sound
|
34
|
+
aps.merge!(self.other) if self.other
|
35
|
+
aps.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
# require 'cgi'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module GCM
|
6
|
+
include HTTParty
|
7
|
+
|
8
|
+
@host = 'https://android.googleapis.com/gcm/send'
|
9
|
+
@format = :json
|
10
|
+
@key = nil
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :host, :format, :key
|
14
|
+
|
15
|
+
def key(identity = nil)
|
16
|
+
if @key.is_a?(Hash)
|
17
|
+
raise %{If your key is a hash of keys you'l need to pass a identifier to the notification!} if identity.nil?
|
18
|
+
return @key[identity]
|
19
|
+
else
|
20
|
+
return @key
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def key_identities
|
25
|
+
if @key.is_a?(Hash)
|
26
|
+
return @key.keys
|
27
|
+
else
|
28
|
+
return nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.send_notification(device_tokens, data = {}, options = {})
|
34
|
+
n = GCM::Notification.new(device_tokens, data, options)
|
35
|
+
self.send_notifications([n])
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.send_notifications(notifications)
|
39
|
+
responses = []
|
40
|
+
notifications.each do |n|
|
41
|
+
responses << self.prepare_and_send(n)
|
42
|
+
end
|
43
|
+
responses
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def self.prepare_and_send(n)
|
49
|
+
if n.device_tokens.count < 1 || n.device_tokens.count > 1000
|
50
|
+
raise "Number of device_tokens invalid, keep it betwen 1 and 1000"
|
51
|
+
end
|
52
|
+
if !n.collapse_key.nil? && n.time_to_live.nil?
|
53
|
+
raise %q{If you are defining a "colapse key" you need a "time to live"}
|
54
|
+
end
|
55
|
+
if @key.is_a?(Hash) && n.identity.nil?
|
56
|
+
raise %{If your key is a hash of keys you'l need to pass a identifier to the notification!}
|
57
|
+
end
|
58
|
+
|
59
|
+
if self.format == :json
|
60
|
+
self.send_push_as_json(n)
|
61
|
+
elsif self.format == :text
|
62
|
+
self.send_push_as_plain_text(n)
|
63
|
+
else
|
64
|
+
raise "Invalid format"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.send_push_as_json(n)
|
69
|
+
headers = {
|
70
|
+
'Authorization' => "key=#{ self.key(n.identity) }",
|
71
|
+
'Content-Type' => 'application/json',
|
72
|
+
}
|
73
|
+
body = {
|
74
|
+
:registration_ids => n.device_tokens,
|
75
|
+
:data => n.data,
|
76
|
+
:collapse_key => n.collapse_key,
|
77
|
+
:time_to_live => n.time_to_live,
|
78
|
+
:delay_while_idle => n.delay_while_idle
|
79
|
+
}
|
80
|
+
return self.send_to_server(headers, body.to_json)
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.send_push_as_plain_text(n)
|
84
|
+
raise "Still has to be done: http://developer.android.com/guide/google/gcm/gcm.html"
|
85
|
+
headers = {
|
86
|
+
# TODO: Aceitar key ser um hash
|
87
|
+
'Authorization' => "key=#{ self.key(n.identity) }",
|
88
|
+
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
|
89
|
+
}
|
90
|
+
return self.send_to_server(headers, body)
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.send_to_server(headers, body)
|
94
|
+
params = {:headers => headers, :body => body}
|
95
|
+
response = self.post(self.host, params)
|
96
|
+
return build_response(response)
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.build_response(response)
|
100
|
+
case response.code
|
101
|
+
when 200
|
102
|
+
{:response => 'success', :body => JSON.parse(response.body), :headers => response.headers, :status_code => response.code}
|
103
|
+
when 400
|
104
|
+
{:response => 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.', :status_code => response.code}
|
105
|
+
when 401
|
106
|
+
{:response => 'There was an error authenticating the sender account.', :status_code => response.code}
|
107
|
+
when 500
|
108
|
+
{:response => 'There was an internal error in the GCM server while trying to process the request.', :status_code => response.code}
|
109
|
+
when 503
|
110
|
+
{:response => 'Server is temporarily unavailable.', :status_code => response.code}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module GCM
|
2
|
+
class Notification
|
3
|
+
attr_accessor :device_tokens, :data, :collapse_key, :time_to_live, :delay_while_idle, :identity
|
4
|
+
|
5
|
+
def initialize(tokens, data, options = {})
|
6
|
+
self.device_tokens = tokens
|
7
|
+
self.data = data
|
8
|
+
|
9
|
+
@collapse_key = options[:collapse_key]
|
10
|
+
@time_to_live = options[:time_to_live]
|
11
|
+
@delay_while_idle = options[:delay_while_idle]
|
12
|
+
@identity = options[:identity]
|
13
|
+
end
|
14
|
+
|
15
|
+
def device_tokens=(tokens)
|
16
|
+
if tokens.is_a?(Array)
|
17
|
+
@device_tokens = tokens
|
18
|
+
elsif tokens.is_a?(String)
|
19
|
+
@device_tokens = [tokens]
|
20
|
+
else
|
21
|
+
raise "device_tokens needs to be either a hash or string"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def data=(data)
|
26
|
+
if data.is_a?(Hash)
|
27
|
+
@data = data
|
28
|
+
else
|
29
|
+
raise "data parameter must be the tpe of Hash"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def delay_while_idle=(delay_while_idle)
|
34
|
+
@delay_while_idle = (delay_while_idle == true || delay_while_idle == :true)
|
35
|
+
end
|
36
|
+
|
37
|
+
def time_to_live=(time_to_live)
|
38
|
+
if time_to_live.is_a?(Integer)
|
39
|
+
@time_to_live = time_to_live
|
40
|
+
else
|
41
|
+
raise %q{"time_to_live" must be seconds as an integer value, like "100"}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
data/pushmeup.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "pushmeup/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'chaione-pushmeup'
|
7
|
+
s.version = Pushmeup::VERSION
|
8
|
+
s.authors = ["Nicos Karalis", "Chris Mason", "Yujing Zheng"]
|
9
|
+
s.email = ["nicoskaralis@me.com", "chris.mason@chaione.com", "yujing.zheng@chaione.com"]
|
10
|
+
|
11
|
+
s.homepage = "https://github.com/chaione/pushmeup"
|
12
|
+
s.summary = %q{Send push notifications to Apple devices through ANPS and Android devices through GCM}
|
13
|
+
s.description = <<-DESC
|
14
|
+
This gem is a wrapper to send push notifications to devices.
|
15
|
+
Currently it only sends to Android or iOS devices, but more platforms will be added soon.
|
16
|
+
|
17
|
+
With APNS (Apple Push Notifications Service) you can send push notifications to Apple devices.
|
18
|
+
With GCM (Google Cloud Messaging) you can send push notifications to Android devices.
|
19
|
+
|
20
|
+
Forked from https://github.com/NicosKaralis/pushmeup
|
21
|
+
DESC
|
22
|
+
|
23
|
+
s.license = 'MIT'
|
24
|
+
|
25
|
+
s.files = `git ls-files`.split("\n")
|
26
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
27
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
28
|
+
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
|
31
|
+
s.add_dependency 'httparty'
|
32
|
+
s.add_dependency 'json'
|
33
|
+
|
34
|
+
s.add_development_dependency 'rake'
|
35
|
+
s.add_development_dependency 'rspec'
|
36
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Pushmeup do
|
4
|
+
describe "APNS" do
|
5
|
+
it "should have a APNS object" do
|
6
|
+
defined?(APNS).should_not be_false
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not forget the APNS default parameters" do
|
10
|
+
APNS.host.should == "gateway.sandbox.push.apple.com"
|
11
|
+
APNS.port.should == 2195
|
12
|
+
APNS.pem.should be_equal(nil)
|
13
|
+
APNS.pass.should be_equal(nil)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "GCM" do
|
19
|
+
it "should have a GCM object" do
|
20
|
+
defined?(GCM).should_not be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "Notifications" do
|
24
|
+
|
25
|
+
before do
|
26
|
+
@options = {:data => "dummy data"}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should allow only notifications with device_tokens as array" do
|
30
|
+
n = GCM::Notification.new("id", @options)
|
31
|
+
n.device_tokens.is_a?(Array).should be_true
|
32
|
+
|
33
|
+
n.device_tokens = ["a" "b", "c"]
|
34
|
+
n.device_tokens.is_a?(Array).should be_true
|
35
|
+
|
36
|
+
n.device_tokens = "a"
|
37
|
+
n.device_tokens.is_a?(Array).should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should allow only notifications with data as hash with :data root" do
|
41
|
+
n = GCM::Notification.new("id", {:data => "data"})
|
42
|
+
|
43
|
+
n.data.is_a?(Hash).should be_true
|
44
|
+
n.data.should == {:data => "data"}
|
45
|
+
|
46
|
+
n.data = {:a => ["a", "b", "c"]}
|
47
|
+
n.data.is_a?(Hash).should be_true
|
48
|
+
n.data.should == {:a => ["a", "b", "c"]}
|
49
|
+
|
50
|
+
n.data = {:a => "a"}
|
51
|
+
n.data.is_a?(Hash).should be_true
|
52
|
+
n.data.should == {:a => "a"}
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pushmeup'
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chaione-pushmeup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicos Karalis
|
8
|
+
- Chris Mason
|
9
|
+
- Yujing Zheng
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-10-09 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: httparty
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: json
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rake
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rspec
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
description: " This gem is a wrapper to send push notifications
|
72
|
+
to devices.\n Currently it only sends to Android or iOS devices,
|
73
|
+
but more platforms will be added soon.\n\n With APNS (Apple
|
74
|
+
Push Notifications Service) you can send push notifications to Apple devices.\n
|
75
|
+
\ With GCM (Google Cloud Messaging) you can send push notifications
|
76
|
+
to Android devices.\n \n Forked from
|
77
|
+
https://github.com/NicosKaralis/pushmeup\n"
|
78
|
+
email:
|
79
|
+
- nicoskaralis@me.com
|
80
|
+
- chris.mason@chaione.com
|
81
|
+
- yujing.zheng@chaione.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .rvmrc
|
88
|
+
- .travis.yml
|
89
|
+
- Gemfile
|
90
|
+
- Keychain Access.jpg
|
91
|
+
- LICENSE
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/pushmeup.rb
|
95
|
+
- lib/pushmeup/android.rb
|
96
|
+
- lib/pushmeup/apns/core.rb
|
97
|
+
- lib/pushmeup/apns/notification.rb
|
98
|
+
- lib/pushmeup/apple.rb
|
99
|
+
- lib/pushmeup/gcm/core.rb
|
100
|
+
- lib/pushmeup/gcm/notification.rb
|
101
|
+
- lib/pushmeup/version.rb
|
102
|
+
- pushmeup.gemspec
|
103
|
+
- spec/lib/pushmeup_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: https://github.com/chaione/pushmeup
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.0.3
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Send push notifications to Apple devices through ANPS and Android devices
|
129
|
+
through GCM
|
130
|
+
test_files:
|
131
|
+
- spec/lib/pushmeup_spec.rb
|
132
|
+
- spec/spec_helper.rb
|