apns_provider_api 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +67 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/apns_provider_api.gemspec +23 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/apns_provider_api/client.rb +129 -0
- data/lib/apns_provider_api/connection.rb +79 -0
- data/lib/apns_provider_api/example.rb +21 -0
- data/lib/apns_provider_api/notification.rb +108 -0
- data/lib/apns_provider_api/version.rb +3 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 513dac1f43bcb7d41e2c9dbd6f0cdf9035886b3a
|
4
|
+
data.tar.gz: 9f37404cff7df5f8ab8d84a9a887928f6ef79f83
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ab7f04d0fd860c0b4551006bb2ced6791b694ad37320e48d50ca192ea613068c2a9965bb403ab525ff36c8a1de05ae53ea8865646337f58f4568f1287a0f32b
|
7
|
+
data.tar.gz: 520552af055c0f8185057e31f1a1b7318e1228c3c69c09cc4ac7f32f729027eb53c532fe1dae35942817f1884ac1c23fc49880a9753598eea680f3a99a79ac7a
|
data/.gitignore
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
Skip to content
|
2
|
+
This repository
|
3
|
+
Search
|
4
|
+
Pull requests
|
5
|
+
Issues
|
6
|
+
Gist
|
7
|
+
@mieps
|
8
|
+
Watch 1,409
|
9
|
+
Star 31,103
|
10
|
+
Fork 11,688 github/gitignore
|
11
|
+
Code Pull requests 160 Pulse Graphs
|
12
|
+
Branch: master Find file Copy pathgitignore/Global/JetBrains.gitignore
|
13
|
+
38d6cac on Oct 29, 2015
|
14
|
+
@tsteinholz tsteinholz Update JetBrains.gitignore
|
15
|
+
16 contributors @samskiter @thunsaker @arcresu @victorhaggqvist @yatskevich @quadroid @jturmel @ssinss @GingerGeek @Fuhrmann @ergl @drothmaler @Nainterceptor @cemmersb @tsteinholz @shiftkey
|
16
|
+
RawBlameHistory 49 lines (36 sloc) 905 Bytes
|
17
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
18
|
+
|
19
|
+
*.iml
|
20
|
+
|
21
|
+
## Directory-based project format:
|
22
|
+
.idea/
|
23
|
+
# if you remove the above rule, at least ignore the following:
|
24
|
+
|
25
|
+
# User-specific stuff:
|
26
|
+
# .idea/workspace.xml
|
27
|
+
# .idea/tasks.xml
|
28
|
+
# .idea/dictionaries
|
29
|
+
# .idea/shelf
|
30
|
+
|
31
|
+
# Sensitive or high-churn files:
|
32
|
+
# .idea/dataSources.ids
|
33
|
+
# .idea/dataSources.xml
|
34
|
+
# .idea/sqlDataSources.xml
|
35
|
+
# .idea/dynamic.xml
|
36
|
+
# .idea/uiDesigner.xml
|
37
|
+
|
38
|
+
# Gradle:
|
39
|
+
# .idea/gradle.xml
|
40
|
+
# .idea/libraries
|
41
|
+
|
42
|
+
# Mongo Explorer plugin:
|
43
|
+
# .idea/mongoSettings.xml
|
44
|
+
|
45
|
+
## File-based project format:
|
46
|
+
*.ipr
|
47
|
+
*.iws
|
48
|
+
|
49
|
+
## Plugin-specific files:
|
50
|
+
|
51
|
+
# IntelliJ
|
52
|
+
/out/
|
53
|
+
|
54
|
+
# mpeltonen/sbt-idea plugin
|
55
|
+
.idea_modules/
|
56
|
+
|
57
|
+
# JIRA plugin
|
58
|
+
atlassian-ide-plugin.xml
|
59
|
+
|
60
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
61
|
+
com_crashlytics_export_strings.xml
|
62
|
+
crashlytics.properties
|
63
|
+
crashlytics-build.properties
|
64
|
+
fabric.properties
|
65
|
+
Status API Training Shop Blog About Pricing
|
66
|
+
© 2016 GitHub, Inc. Terms Privacy Security Contact Help
|
67
|
+
Save Copy Share
|
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/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
apns_provider_api (1.0.0)
|
5
|
+
http-2 (~> 0.7.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
http-2 (0.7.0)
|
11
|
+
rake (10.4.2)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
apns_provider_api!
|
18
|
+
bundler (~> 1.10)
|
19
|
+
rake (~> 10.0)
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
1.11.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Michel Epsztejn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# ApnsProviderApi
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/apns_provider_api`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'apns_provider_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install apns_provider_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/apns_provider_api. 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.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'apns_provider_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "apns_provider_api"
|
8
|
+
spec.version = ApnsProviderApi::VERSION
|
9
|
+
spec.authors = ["Michel Epsztejn"]
|
10
|
+
spec.email = ["michel.epsz@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Apple Push Notifications using the HTTP/2 Provider API}
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
spec.add_dependency 'http-2', '~> 0.7.0'
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "apns_provider_api"
|
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
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'http/2'
|
2
|
+
require 'uri'
|
3
|
+
require 'socket'
|
4
|
+
require 'openssl'
|
5
|
+
|
6
|
+
module ApnsProviderApi
|
7
|
+
|
8
|
+
APPLE_DEVELOPMENT_GATEWAY_URI = "https://api.development.push.apple.com:443"
|
9
|
+
APPLE_PRODUCTION_GATEWAY_URI = "https://api.push.apple.com:443"
|
10
|
+
|
11
|
+
class Client
|
12
|
+
attr_accessor :gateway_uri, :feedback_uri, :certificate, :passphrase, :timeout, :split_size
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def development
|
16
|
+
client = self.new
|
17
|
+
client.gateway_uri = APPLE_DEVELOPMENT_GATEWAY_URI
|
18
|
+
client
|
19
|
+
end
|
20
|
+
|
21
|
+
def production
|
22
|
+
client = self.new
|
23
|
+
client.gateway_uri = APPLE_PRODUCTION_GATEWAY_URI
|
24
|
+
client
|
25
|
+
end
|
26
|
+
|
27
|
+
def mock
|
28
|
+
client = self.new
|
29
|
+
client.gateway_uri = "apn://127.0.0.1:2195"
|
30
|
+
client
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
@gateway_uri = ENV['APN_GATEWAY_URI']
|
36
|
+
@certificate = File.read(ENV['APN_CERTIFICATE']) if ENV['APN_CERTIFICATE']
|
37
|
+
@passphrase = ENV['APN_CERTIFICATE_PASSPHRASE']
|
38
|
+
@pid = Process.pid
|
39
|
+
@failed_notifications, @failed_streams, @notifications = [], [], []
|
40
|
+
end
|
41
|
+
|
42
|
+
def enqueue(notifications)
|
43
|
+
@split_size ||= 500
|
44
|
+
notifications.each_slice(@split_size) do |group|
|
45
|
+
push(group)
|
46
|
+
end
|
47
|
+
@failed_notifications
|
48
|
+
end
|
49
|
+
|
50
|
+
def push(notifications)
|
51
|
+
return if notifications.empty?
|
52
|
+
|
53
|
+
@notifications = notifications.flatten
|
54
|
+
@counter = 0
|
55
|
+
|
56
|
+
head = {
|
57
|
+
':scheme' => 'https',
|
58
|
+
':method' => 'POST'
|
59
|
+
# ':apns-expiration' => 0 #do not store for sending late
|
60
|
+
#'content-length' => notification.message.size
|
61
|
+
}
|
62
|
+
|
63
|
+
Connection.open(@gateway_uri, @certificate, @passphrase) do |connection|
|
64
|
+
ssl_sock = connection.ssl
|
65
|
+
# events(connection.http2client, ssl_sock)
|
66
|
+
|
67
|
+
@notifications.each_with_index do |notification, index|
|
68
|
+
connection.open?
|
69
|
+
head[':path'] = "/3/device/#{notification.token}"
|
70
|
+
head[':apns-id'] = notification.uuid
|
71
|
+
|
72
|
+
stream = connection.new_stream
|
73
|
+
|
74
|
+
# because apple is not returning the uuid when push fails,
|
75
|
+
# i'm replacing uuid to stream id
|
76
|
+
notification.uuid = stream.id
|
77
|
+
|
78
|
+
stream.on(:headers) do |h|
|
79
|
+
# puts "headers: #{h} - stream_id: #{stream.id}"
|
80
|
+
read_headers(Hash[*h.flatten], stream)
|
81
|
+
end
|
82
|
+
stream.on(:data) do |d|
|
83
|
+
# puts "data: #{d}"
|
84
|
+
# puts "#{JSON.parse(d)} - #{stream.id.to_s}"
|
85
|
+
read_body(JSON.parse(d), stream)
|
86
|
+
end
|
87
|
+
stream.headers(head, end_stream: false)
|
88
|
+
stream.data(notification.payload.to_json)
|
89
|
+
end
|
90
|
+
|
91
|
+
# keep_reading = true
|
92
|
+
|
93
|
+
while !ssl_sock.closed? && !ssl_sock.eof?
|
94
|
+
data = ssl_sock.read_nonblock(1024)
|
95
|
+
# puts "Received bytes: #{data.unpack("H*").first}"
|
96
|
+
begin
|
97
|
+
connection.http2client << data
|
98
|
+
return if @counter == @notifications.size
|
99
|
+
rescue => e
|
100
|
+
puts "Exception: #{e}, #{e.message} - closing socket."
|
101
|
+
ssl_sock.close
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def read_headers(headers, stream)
|
108
|
+
# puts headers[':status']
|
109
|
+
if headers[':status'].to_i == 200
|
110
|
+
# when notification succeeds, we wont receive body
|
111
|
+
increase_counter
|
112
|
+
else
|
113
|
+
@failed_streams << stream
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def read_body(response, stream)
|
118
|
+
increase_counter
|
119
|
+
return unless @failed_streams.include?(stream)
|
120
|
+
notification = @notifications.select{|n| n.uuid == stream.id}[0]
|
121
|
+
notification.error_message = response['reason']
|
122
|
+
@failed_notifications << notification
|
123
|
+
end
|
124
|
+
|
125
|
+
def increase_counter
|
126
|
+
@counter += 1
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'http/2'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module ApnsProviderApi
|
5
|
+
class Connection
|
6
|
+
extend Forwardable
|
7
|
+
def_delegators :@ssl, :read, :write
|
8
|
+
def_delegators :@uri, :scheme, :host, :port
|
9
|
+
# def_delegators :@stream, :headers, :data
|
10
|
+
|
11
|
+
attr_reader :ssl, :socket, :certificate, :passphrase, :http2client
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def open(uri, certificate, passphrase)
|
15
|
+
return unless block_given?
|
16
|
+
|
17
|
+
connection = new(uri, certificate, passphrase)
|
18
|
+
connection.open
|
19
|
+
|
20
|
+
yield connection
|
21
|
+
|
22
|
+
connection.close
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(uri, certificate, passphrase)
|
27
|
+
@uri = URI(uri)
|
28
|
+
@certificate = certificate
|
29
|
+
@passphrase = passphrase
|
30
|
+
@http2client = HTTP2::Client.new
|
31
|
+
events
|
32
|
+
@http2client
|
33
|
+
end
|
34
|
+
|
35
|
+
def open
|
36
|
+
return false if open?
|
37
|
+
|
38
|
+
@socket = TCPSocket.new(@uri.host, @uri.port)
|
39
|
+
context = OpenSSL::SSL::SSLContext.new
|
40
|
+
context.key = OpenSSL::PKey::RSA.new(@certificate, @passphrase)
|
41
|
+
context.cert = OpenSSL::X509::Certificate.new(certificate)
|
42
|
+
|
43
|
+
@ssl = OpenSSL::SSL::SSLSocket.new(@socket, context)
|
44
|
+
@ssl.sync_close = true
|
45
|
+
@ssl.hostname = @uri.hostname
|
46
|
+
@ssl.connect
|
47
|
+
end
|
48
|
+
|
49
|
+
def open?
|
50
|
+
not (@ssl and @socket).nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
def close
|
54
|
+
return false if closed?
|
55
|
+
@socket.close
|
56
|
+
@socket = nil
|
57
|
+
|
58
|
+
@ssl.close
|
59
|
+
@ssl = nil
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def closed?
|
64
|
+
not open?
|
65
|
+
end
|
66
|
+
|
67
|
+
def new_stream
|
68
|
+
@http2client.new_stream
|
69
|
+
end
|
70
|
+
|
71
|
+
def events
|
72
|
+
@http2client.on(:frame) do |bytes|
|
73
|
+
# puts "Sending bytes: #{bytes.unpack("H*").first}"
|
74
|
+
ssl.print bytes
|
75
|
+
ssl.flush
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative 'client'
|
2
|
+
require_relative 'connection'
|
3
|
+
require_relative 'notification'
|
4
|
+
|
5
|
+
APN = ApnsProviderApi::Client.development
|
6
|
+
TOKEN_ARRAY = ["1234", "4321"]
|
7
|
+
APN.certificate = File.read("/development.pem")
|
8
|
+
APN.passphrase = Filre.read("/passphrase.txt")
|
9
|
+
|
10
|
+
notification_array = []
|
11
|
+
|
12
|
+
TOKEN_ARRAY.each do |token|
|
13
|
+
notification = ApnsProviderApi::Notification.new(badge: 1,
|
14
|
+
device: token,
|
15
|
+
alert: "TEST #{token}",
|
16
|
+
sound: "sosumi.aiff")
|
17
|
+
notification_array << notification
|
18
|
+
end
|
19
|
+
|
20
|
+
failed_notifications = APN.enqueue(notification_array)
|
21
|
+
puts failed_notifications
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module ApnsProviderApi
|
5
|
+
class Notification
|
6
|
+
class APNSError < RuntimeError
|
7
|
+
# See: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html
|
8
|
+
CODES = {
|
9
|
+
200 => "Success",
|
10
|
+
400 => "Bad request",
|
11
|
+
403 => "There was an error with the certificate",
|
12
|
+
405 => "The request used a bad :method value. Only POST requests are supported",
|
13
|
+
410 => "The device token is no longer active for the topic",
|
14
|
+
413 => "The notification payload was too large",
|
15
|
+
429 => "The server received too many requests for the same device token",
|
16
|
+
500 => "Internal server error",
|
17
|
+
503 => "The server is shutting down and unavailable"
|
18
|
+
}
|
19
|
+
|
20
|
+
attr_reader :code
|
21
|
+
|
22
|
+
def initialize(code)
|
23
|
+
raise ArgumentError unless CODES.include?(code)
|
24
|
+
super(CODES[code])
|
25
|
+
@code = code
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# MAXIMUM_PAYLOAD_SIZE = 4096 already accepted by provider API but not for iOS apps
|
30
|
+
MAXIMUM_PAYLOAD_SIZE = 2048
|
31
|
+
|
32
|
+
attr_accessor :token, :alert, :badge, :sound, :category, :content_available, :custom_data, :id, :expiry, :priority, :uuid, :error_message
|
33
|
+
attr_reader :sent_at
|
34
|
+
# attr_writer :apns_error_code
|
35
|
+
|
36
|
+
alias :device :token
|
37
|
+
alias :device= :token=
|
38
|
+
|
39
|
+
def initialize(options = {})
|
40
|
+
@token = options.delete(:token) || options.delete(:device)
|
41
|
+
raise 'invalid token' if @token && @token.empty?
|
42
|
+
@badge = options.delete(:badge)
|
43
|
+
@sound = options.delete(:sound)
|
44
|
+
@category = options.delete(:category)
|
45
|
+
@expiry = options.delete(:expiry)
|
46
|
+
@id = options.delete(:id)
|
47
|
+
@priority = options.delete(:priority)
|
48
|
+
@content_available = options.delete(:content_available)
|
49
|
+
dottize = options.delete(:dottize)
|
50
|
+
@custom_data = options
|
51
|
+
alert = options.delete(:alert)
|
52
|
+
available_bytes = MAXIMUM_PAYLOAD_SIZE - payload.to_s.bytesize
|
53
|
+
@alert = trim_alert(alert, available_bytes, dottize)
|
54
|
+
generate_uuid(options.delete(:uuid))
|
55
|
+
end
|
56
|
+
|
57
|
+
def payload
|
58
|
+
json = {}.merge(@custom_data || {}).inject({}){|h,(k,v)| h[k.to_s] = v; h}
|
59
|
+
|
60
|
+
json['aps'] ||= {}
|
61
|
+
json['aps']['alert'] = @alert if @alert
|
62
|
+
json['aps']['badge'] = @badge.to_i rescue 0 if @badge
|
63
|
+
json['aps']['sound'] = @sound if @sound
|
64
|
+
json['aps']['category'] = @category if @category
|
65
|
+
json['aps']['content-available'] = 1 if @content_available
|
66
|
+
|
67
|
+
json
|
68
|
+
end
|
69
|
+
|
70
|
+
def mark_as_sent!
|
71
|
+
@sent_at = Time.now
|
72
|
+
end
|
73
|
+
|
74
|
+
def mark_as_unsent!
|
75
|
+
@sent_at = nil
|
76
|
+
end
|
77
|
+
|
78
|
+
def sent?
|
79
|
+
!!@sent_at
|
80
|
+
end
|
81
|
+
|
82
|
+
def trim_alert(alert, available_bytes, dottize)
|
83
|
+
if alert.size > available_bytes
|
84
|
+
raise "the notification payload was too large \nTip: use dottize option to auto trim the notification alert" unless dottize
|
85
|
+
chars_to_delete = alert.size - available_bytes
|
86
|
+
alert = dottize_no_broken_words(alert, alert.size - chars_to_delete)
|
87
|
+
end
|
88
|
+
alert
|
89
|
+
end
|
90
|
+
|
91
|
+
# def error
|
92
|
+
# APNSError.new(@apns_error_code) if @apns_error_code and @apns_error_code.nonzero?
|
93
|
+
# end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def generate_uuid(uuid=nil)
|
98
|
+
uuid = SecureRandom.uuid unless uuid
|
99
|
+
@uuid = uuid
|
100
|
+
end
|
101
|
+
|
102
|
+
def dottize_no_broken_words(string, limit=3)
|
103
|
+
size = string.size
|
104
|
+
size > limit ? "#{string[0,string[0,limit-3].rindex(" ") || limit-3]}..." : string
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apns_provider_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michel Epsztejn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http-2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.7.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.7.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- michel.epsz@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- apns_provider_api.gemspec
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/apns_provider_api/client.rb
|
74
|
+
- lib/apns_provider_api/connection.rb
|
75
|
+
- lib/apns_provider_api/example.rb
|
76
|
+
- lib/apns_provider_api/notification.rb
|
77
|
+
- lib/apns_provider_api/version.rb
|
78
|
+
homepage:
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.5
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Apple Push Notifications using the HTTP/2 Provider API
|
102
|
+
test_files: []
|