apns-persistent 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +6 -0
- data/apns-persistent.gemspec +25 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/exe/push +40 -0
- data/exe/push_daemon +57 -0
- data/exe/push_once +47 -0
- data/lib/apns/persistent.rb +11 -0
- data/lib/apns/persistent/client.rb +20 -0
- data/lib/apns/persistent/connection.rb +54 -0
- data/lib/apns/persistent/feedback_client.rb +38 -0
- data/lib/apns/persistent/push_client.rb +125 -0
- data/lib/apns/persistent/version.rb +5 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b67a066d44af6faeb712b35fa21802a5d2c5b461
|
4
|
+
data.tar.gz: 9b61acc873a314477318c59a85cc71f0868b8ae2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f3c23c9d6229286ff775d89969bf21f518a22b0c10892a4baf4d8b4908c0e37351f3fc06b67bf8373a0911bc12067c2ede55ef78e9d039d84f76695cd8ff7f1
|
7
|
+
data.tar.gz: bed275d750dabc3d6afbec8440752308dca125ebbc815b5cb0df899a8fd0f33d8d4c41b8dab846b8e017c72a7357bf491a8a133d0c0dc27475042b16f982a8f4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Koji Murata (https://github.com/malt03)
|
4
|
+
Copyright (c) 2012–2015 Mattt Thompson (http://mattt.me/)
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# Apns::Persistent
|
2
|
+
|
3
|
+
apns-persisitent is referencing [houston](https://rubygems.org/gems/houston/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'apns-persistent'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install apns-persistent
|
20
|
+
|
21
|
+
## Push Usage
|
22
|
+
### Recommend
|
23
|
+
```ruby
|
24
|
+
c = Apns::Persistent::PushClient.new(certificate: '/path/to/apple_push_notification.pem', sandbox: true)
|
25
|
+
c.open
|
26
|
+
|
27
|
+
thread = c.regist_error_handle do |command, status, id|
|
28
|
+
#error handle
|
29
|
+
puts "Send Error! command:#{command} status:#{status} id:#{id}"
|
30
|
+
end
|
31
|
+
|
32
|
+
c.push(token: '88189fcf 62a1b2eb b7cb1435 597e734e a90da4ce 6196a9b3 309a5421 4c6259e',
|
33
|
+
alert: 'foobar',
|
34
|
+
sound: 'default',
|
35
|
+
id: 1)
|
36
|
+
|
37
|
+
begin
|
38
|
+
thread.join
|
39
|
+
rescue Interrupt
|
40
|
+
c.close
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
### Without Thread
|
45
|
+
```ruby
|
46
|
+
c = Apns::Persistent::PushClient.new(certificate: '/path/to/apple_push_notification.pem', sandbox: true)
|
47
|
+
c.open
|
48
|
+
c.push(token: '88189fcf 62a1b2eb b7cb1435 597e734e a90da4ce 6196a9b3 309a5421 4c6259e',
|
49
|
+
alert: 'foobar',
|
50
|
+
sound: 'default',
|
51
|
+
id: 1) do |command, status, id|
|
52
|
+
#error handle
|
53
|
+
puts "Send Error! command:#{command} status:#{status} id:#{id}"
|
54
|
+
end
|
55
|
+
c.close
|
56
|
+
```
|
57
|
+
|
58
|
+
### Without Persistent Connections
|
59
|
+
```ruby
|
60
|
+
Apns::Persistent::PushClient.push_once(certificate: '/path/to/apple_push_notification.pem',
|
61
|
+
token: '88189fcf 62a1b2eb b7cb1435 597e734e a90da4ce 6196a9b3 309a5421 4c6259e9',
|
62
|
+
alert: 'foobar',
|
63
|
+
sound: 'default',
|
64
|
+
id: 1) do |command, status, id|
|
65
|
+
#error handle
|
66
|
+
puts "Send Error! command:#{command} status:#{status} id:#{id}"
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
## Feedback API Usage
|
71
|
+
### Get unregistered devices
|
72
|
+
```ruby
|
73
|
+
c = Apns::Persistent::FeedbackClient.new(certificate: '/path/to/apple_push_notification.pem', sandbox: true)
|
74
|
+
c.open
|
75
|
+
devices = c.unregistered_devices
|
76
|
+
c.close
|
77
|
+
```
|
78
|
+
|
79
|
+
### Get unregistered device tokens
|
80
|
+
```ruby
|
81
|
+
c = Apns::Persistent::FeedbackClient.new(certificate: '/path/to/apple_push_notification.pem', sandbox: true)
|
82
|
+
c.open
|
83
|
+
device_tokens = c.unregistered_device_tokens
|
84
|
+
c.close
|
85
|
+
```
|
86
|
+
|
87
|
+
### Get unregistered devices once
|
88
|
+
```ruby
|
89
|
+
devices = Apns::Persistent::FeedbackClient.unregistered_devices_once(certificate: '/path/to/apple_push_notification.pem', sandbox: true)
|
90
|
+
```
|
91
|
+
|
92
|
+
### Get unregistered device tokens once
|
93
|
+
```ruby
|
94
|
+
device_tokens = Apns::Persistent::FeedbackClient.unregistered_device_tokens_once(certificate: '/path/to/apple_push_notification.pem', sandbox: true)
|
95
|
+
```
|
96
|
+
|
97
|
+
## Command Line Tools
|
98
|
+
### Launch Daemon
|
99
|
+
```console
|
100
|
+
$ push_daemon --pemfile <path> [--sandbox]
|
101
|
+
```
|
102
|
+
### Push after launch daemon
|
103
|
+
```console
|
104
|
+
$ push --token <token> --alert "Hello" --badge 2 --sound "default"
|
105
|
+
```
|
106
|
+
|
107
|
+
### Push once
|
108
|
+
```console
|
109
|
+
$ push_once --pemfile <path> [--sandbox] --token <token> --alert "Hello" --sound "default" --badge 2
|
110
|
+
```
|
111
|
+
|
112
|
+
## Contributing
|
113
|
+
|
114
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/malt03/apns-persistent. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
115
|
+
|
116
|
+
|
117
|
+
## License
|
118
|
+
|
119
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
120
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'apns/persistent/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "apns-persistent"
|
8
|
+
spec.version = Apns::Persistent::VERSION
|
9
|
+
spec.authors = ["malt03"]
|
10
|
+
spec.email = ["malt.koji@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Send Apple Push Notifications"
|
13
|
+
spec.description = "apns-persistent is a gem for sending APNs and easy to manage connections. "
|
14
|
+
spec.homepage = "https://github.com/malt03/apns-persistent"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "apns/persistent"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/push
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'yaml'
|
5
|
+
require 'socket'
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
require 'apns/persistent'
|
9
|
+
|
10
|
+
params = Hash[ARGV.getopts('',
|
11
|
+
'help',
|
12
|
+
'host:',
|
13
|
+
'port:',
|
14
|
+
'token:',
|
15
|
+
'alert:',
|
16
|
+
'badge:',
|
17
|
+
'sound:',
|
18
|
+
'category:',
|
19
|
+
'content_available',
|
20
|
+
'payload_yaml:',
|
21
|
+
'id:',
|
22
|
+
'expiry:',
|
23
|
+
'priority:').map { |k, v| [k.to_sym, v] }]
|
24
|
+
|
25
|
+
if params.delete(:help)
|
26
|
+
puts 'Usage: push [--host host] [--port port] [--token string] [--alert string] [--badge num] [--sound string] [--category string] [--content_available] [--payload_yaml path] [--id num] [--expiry time] [--priority num]'
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
params[:id] = params[:id].to_i if params[:id]
|
31
|
+
params.delete(:payload_yaml).tap do |file|
|
32
|
+
params[:custom_payload] = YAML.load(File.read(file)) if file
|
33
|
+
end
|
34
|
+
|
35
|
+
sock = TCPSocket.open(params.delete(:host) || 'localhost', params.delete(:port) || 20000)
|
36
|
+
yml = params.to_yaml
|
37
|
+
sock.write([yml.length].pack('I'))
|
38
|
+
sock.write(yml)
|
39
|
+
puts sock.gets
|
40
|
+
sock.close
|
data/exe/push_daemon
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'yaml'
|
5
|
+
require 'socket'
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
require 'apns/persistent'
|
9
|
+
|
10
|
+
def ready(port)
|
11
|
+
puts "\e[32mReady! port=#{port}\e[0m"
|
12
|
+
end
|
13
|
+
|
14
|
+
params = Hash[ARGV.getopts('', 'help', 'port:', 'pemfile:', 'passphrase:', 'sandbox').map { |k, v| [k.to_sym, v] }]
|
15
|
+
|
16
|
+
if params[:help]
|
17
|
+
puts 'Usage: push_daemon [--port port] --pemfile path [--passphrase string] [--sandbox]'
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
|
21
|
+
unless params[:pemfile]
|
22
|
+
puts 'Missing filename (" --help" for help)'
|
23
|
+
exit
|
24
|
+
end
|
25
|
+
unless File.file?(params[:pemfile])
|
26
|
+
puts "#{params[:pemfile]}: No such file or directory"
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
port = params[:port] || 20000
|
31
|
+
|
32
|
+
client = Apns::Persistent::PushClient.new(certificate: params[:pemfile], passphrase: params[:passphrase], sandbox: params[:sandbox])
|
33
|
+
client.open
|
34
|
+
thread = client.regist_error_handle do |command, status, id|
|
35
|
+
puts "\e[31mSend Error! command:#{command} status:#{status} id:#{id}\e[0m"
|
36
|
+
ready(port)
|
37
|
+
end
|
38
|
+
|
39
|
+
gate = TCPServer.open port
|
40
|
+
|
41
|
+
begin
|
42
|
+
loop do
|
43
|
+
ready(port)
|
44
|
+
sock = gate.accept
|
45
|
+
|
46
|
+
length = sock.read(4).unpack('I')[0]
|
47
|
+
data = YAML.load(sock.read(length))
|
48
|
+
|
49
|
+
client.push(data)
|
50
|
+
puts "Sent! id:#{data[:id] || 0}"
|
51
|
+
sock.write("OK\n")
|
52
|
+
sock.close
|
53
|
+
end
|
54
|
+
rescue Interrupt
|
55
|
+
client.close
|
56
|
+
gate.close
|
57
|
+
end
|
data/exe/push_once
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'yaml'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
require 'apns/persistent'
|
8
|
+
|
9
|
+
params = Hash[ARGV.getopts('',
|
10
|
+
'help',
|
11
|
+
'pemfile:',
|
12
|
+
'passphrase:',
|
13
|
+
'sandbox',
|
14
|
+
'token:',
|
15
|
+
'alert:',
|
16
|
+
'badge:',
|
17
|
+
'sound:',
|
18
|
+
'category:',
|
19
|
+
'content_available',
|
20
|
+
'payload_yaml:',
|
21
|
+
'id:',
|
22
|
+
'expiry:',
|
23
|
+
'priority:').map { |k, v| [k.to_sym, v] }]
|
24
|
+
|
25
|
+
if params.delete(:help)
|
26
|
+
puts 'Usage: push [--host host] [--port port] [--token string] [--alert string] [--badge num] [--sound string] [--category string] [--content_available] [--payload_yaml path] [--id num] [--expiry time] [--priority num]'
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
unless params[:pemfile]
|
31
|
+
puts 'Missing filename (" --help" for help)'
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
unless File.file?(params[:pemfile])
|
35
|
+
puts "#{params[:pemfile]}: No such file or directory"
|
36
|
+
exit
|
37
|
+
end
|
38
|
+
|
39
|
+
params[:certificate] = params.delete(:pemfile)
|
40
|
+
params[:id] = params[:id].to_i if params[:id]
|
41
|
+
params.delete(:payload_yaml).tap do |file|
|
42
|
+
params[:custom_payload] = YAML.load(File.read(file)) if file
|
43
|
+
end
|
44
|
+
|
45
|
+
Apns::Persistent::PushClient.push_once(params) do |command, status, id|
|
46
|
+
puts "\e[31mSend Error! command:#{command} status:#{status} id:#{id}\e[0m"
|
47
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "apns/persistent/client"
|
2
|
+
require "apns/persistent/push_client"
|
3
|
+
require "apns/persistent/feedback_client"
|
4
|
+
require "apns/persistent/connection"
|
5
|
+
require "apns/persistent/version"
|
6
|
+
|
7
|
+
# module Apns
|
8
|
+
# module Persistent
|
9
|
+
# # Your code goes here...
|
10
|
+
# end
|
11
|
+
# end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Apns
|
5
|
+
module Persistent
|
6
|
+
class Client
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators :@connection, :open, :close, :opened?, :closed?
|
9
|
+
|
10
|
+
def initialize(certificate: , passphrase: nil, sandbox: false)
|
11
|
+
cer = File.read(certificate)
|
12
|
+
@connection = Connection.new(self.class.gateway_uri(sandbox), cer, passphrase)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.gateway_uri(sandbox)
|
16
|
+
raise 'please inherit'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'socket'
|
3
|
+
require 'openssl'
|
4
|
+
require 'forwardable'
|
5
|
+
|
6
|
+
module Apns
|
7
|
+
module Persistent
|
8
|
+
class Connection
|
9
|
+
extend Forwardable
|
10
|
+
def_delegators :@ssl, :read, :write
|
11
|
+
|
12
|
+
def initialize(uri, certificate, passphrase)
|
13
|
+
@uri = URI(uri)
|
14
|
+
@certificate = certificate
|
15
|
+
@passphrase = passphrase
|
16
|
+
end
|
17
|
+
|
18
|
+
def open
|
19
|
+
@socket = TCPSocket.new(@uri.host, @uri.port)
|
20
|
+
context = OpenSSL::SSL::SSLContext.new
|
21
|
+
context.key = OpenSSL::PKey::RSA.new(@certificate, @passphrase)
|
22
|
+
context.cert = OpenSSL::X509::Certificate.new(@certificate)
|
23
|
+
@ssl = OpenSSL::SSL::SSLSocket.new(@socket, context)
|
24
|
+
@ssl.sync
|
25
|
+
@ssl.connect
|
26
|
+
end
|
27
|
+
|
28
|
+
def close
|
29
|
+
@ssl.close
|
30
|
+
@socket.close
|
31
|
+
@ssl = nil
|
32
|
+
@socket = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def reopen
|
36
|
+
close
|
37
|
+
open
|
38
|
+
end
|
39
|
+
|
40
|
+
def opened?
|
41
|
+
!(@ssl.nil? || @socket.nil?)
|
42
|
+
end
|
43
|
+
|
44
|
+
def closed?
|
45
|
+
!opened?
|
46
|
+
end
|
47
|
+
|
48
|
+
def readable?
|
49
|
+
r, w = IO.select([@ssl], [], [@ssl], 1)
|
50
|
+
(r && r[0])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Apns
|
2
|
+
module Persistent
|
3
|
+
class FeedbackClient < Client
|
4
|
+
def self.unregistered_devices_once(certificate: , sandbox: true)
|
5
|
+
client = Apns::Persistent::FeedbackClient.new(certificate: certificate, sandbox: sandbox)
|
6
|
+
client.open
|
7
|
+
devices = client.unregistered_devices
|
8
|
+
client.close
|
9
|
+
devices
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.unregistered_device_tokens_once(certificate: , sandbox: true)
|
13
|
+
FeedbackClient.unregistered_devices_once(certificate: certificate, sandbox: sandbox).collect { |device| device[:token] }
|
14
|
+
end
|
15
|
+
|
16
|
+
def unregistered_devices
|
17
|
+
raise 'please open' if closed?
|
18
|
+
|
19
|
+
devices = []
|
20
|
+
while line = @connection.read(38)
|
21
|
+
feedback = line.unpack('N1n1H140')
|
22
|
+
timestamp = feedback[0]
|
23
|
+
token = feedback[2].scan(/.{0,8}/).join(' ').strip
|
24
|
+
devices << {token: token, timestamp: timestamp} if token && timestamp
|
25
|
+
end
|
26
|
+
devices
|
27
|
+
end
|
28
|
+
|
29
|
+
def unregistered_device_tokens
|
30
|
+
unregistered_devices.collect { |device| device[:token] }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.gateway_uri(sandbox)
|
34
|
+
sandbox ? "apn://feedback.sandbox.push.apple.com:2196" : "apn://feedback.push.apple.com:2196"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Apns
|
4
|
+
module Persistent
|
5
|
+
class PushClient < Client
|
6
|
+
def self.push_once(certificate: ,
|
7
|
+
passphrase: nil,
|
8
|
+
sandbox: true,
|
9
|
+
|
10
|
+
token: ,
|
11
|
+
alert: nil,
|
12
|
+
badge: nil,
|
13
|
+
sound: nil,
|
14
|
+
category: nil,
|
15
|
+
content_available: true,
|
16
|
+
custom_payload: nil,
|
17
|
+
id: nil,
|
18
|
+
expiry: nil,
|
19
|
+
priority: nil)
|
20
|
+
|
21
|
+
client = PushClient.new(certificate: certificate, passphrase: passphrase, sandbox: sandbox)
|
22
|
+
client.open
|
23
|
+
|
24
|
+
client.push(token: token,
|
25
|
+
alert: alert,
|
26
|
+
badge: badge,
|
27
|
+
sound: sound,
|
28
|
+
category: category,
|
29
|
+
content_available: content_available,
|
30
|
+
custom_payload: custom_payload,
|
31
|
+
id: id,
|
32
|
+
expiry: expiry,
|
33
|
+
priority: priority) do |command, status, return_id|
|
34
|
+
if block_given?
|
35
|
+
yield(command, status, return_id)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
client.close
|
40
|
+
end
|
41
|
+
|
42
|
+
def push(token: ,
|
43
|
+
alert: nil,
|
44
|
+
badge: nil,
|
45
|
+
sound: nil,
|
46
|
+
category: nil,
|
47
|
+
content_available: true,
|
48
|
+
custom_payload: nil,
|
49
|
+
id: nil,
|
50
|
+
expiry: nil,
|
51
|
+
priority: nil)
|
52
|
+
|
53
|
+
raise 'please open' if closed?
|
54
|
+
|
55
|
+
m = PushClient.message(token, alert, badge, sound, category, content_available, custom_payload, id, expiry, priority)
|
56
|
+
@connection.write(m)
|
57
|
+
|
58
|
+
if block_given? && @connection.readable?
|
59
|
+
if error = @connection.read(6)
|
60
|
+
command, status, return_id = error.unpack('ccN')
|
61
|
+
yield(command, status, return_id)
|
62
|
+
@connection.reopen
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def regist_error_handle
|
68
|
+
Thread.new do
|
69
|
+
while error = @connection.read(6)
|
70
|
+
command, status, id = error.unpack('ccN')
|
71
|
+
yield(command, status, id)
|
72
|
+
@connection.reopen
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class << self
|
78
|
+
def gateway_uri(sandbox)
|
79
|
+
sandbox ? "apn://gateway.sandbox.push.apple.com:2195" : "apn://gateway.push.apple.com:2195"
|
80
|
+
end
|
81
|
+
|
82
|
+
def message(token, alert, badge, sound, category, content_available, custom_payload, id, expiry, priority)
|
83
|
+
data = [token_data(token),
|
84
|
+
payload_data(custom_payload, alert, badge, sound, category, content_available),
|
85
|
+
id_data(id),
|
86
|
+
expiration_data(expiry),
|
87
|
+
priority_data(priority)].compact.join
|
88
|
+
[2, data.bytes.count, data].pack('cNa*')
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def token_data(token)
|
94
|
+
[1, 32, token.gsub(/[<\s>]/, '')].pack('cnH64')
|
95
|
+
end
|
96
|
+
|
97
|
+
def payload_data(custom_payload, alert, badge, sound, category, content_available)
|
98
|
+
payload = {}.merge(custom_payload || {}).inject({}){|h,(k,v)| h[k.to_s] = v; h}
|
99
|
+
|
100
|
+
payload['aps'] ||= {}
|
101
|
+
payload['aps']['alert'] = alert if alert
|
102
|
+
payload['aps']['badge'] = badge.to_i rescue 0 if badge
|
103
|
+
payload['aps']['sound'] = sound if sound
|
104
|
+
payload['aps']['category'] = category if category
|
105
|
+
payload['aps']['content-available'] = 1 if content_available
|
106
|
+
|
107
|
+
json = payload.to_json
|
108
|
+
[2, json.bytes.count, json].pack('cna*')
|
109
|
+
end
|
110
|
+
|
111
|
+
def id_data(id)
|
112
|
+
[3, 4, id].pack('cnN') unless id.nil?
|
113
|
+
end
|
114
|
+
|
115
|
+
def expiration_data(expiry)
|
116
|
+
[4, 4, expiry.to_i].pack('cnN') unless expiry.nil?
|
117
|
+
end
|
118
|
+
|
119
|
+
def priority_data(priority)
|
120
|
+
[5, 1, priority].pack('cnc') unless priority.nil?
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apns-persistent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- malt03
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: 'apns-persistent is a gem for sending APNs and easy to manage connections. '
|
56
|
+
email:
|
57
|
+
- malt.koji@gmail.com
|
58
|
+
executables:
|
59
|
+
- push
|
60
|
+
- push_daemon
|
61
|
+
- push_once
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".rspec"
|
67
|
+
- ".travis.yml"
|
68
|
+
- CODE_OF_CONDUCT.md
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- apns-persistent.gemspec
|
74
|
+
- bin/console
|
75
|
+
- bin/setup
|
76
|
+
- exe/push
|
77
|
+
- exe/push_daemon
|
78
|
+
- exe/push_once
|
79
|
+
- lib/apns/persistent.rb
|
80
|
+
- lib/apns/persistent/client.rb
|
81
|
+
- lib/apns/persistent/connection.rb
|
82
|
+
- lib/apns/persistent/feedback_client.rb
|
83
|
+
- lib/apns/persistent/push_client.rb
|
84
|
+
- lib/apns/persistent/version.rb
|
85
|
+
homepage: https://github.com/malt03/apns-persistent
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.2.0
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Send Apple Push Notifications
|
109
|
+
test_files: []
|