-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.rb
64 lines (48 loc) · 1.62 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require 'apnotic'
require 'base64'
require 'sinatra'
require 'dotenv/load' if ENV['RACK_ENV'] == 'development'
CONNECTION_OPTIONS = {
auth_method: :token,
cert_path: StringIO.new(ENV['APNS_AUTH_KEY']),
key_id: ENV['APNS_KEY_ID'],
team_id: ENV['APPLE_TEAM_ID']
}.freeze
SANDBOX_CONNECTION = Apnotic::Connection.development(CONNECTION_OPTIONS.dup)
SANDBOX_CONNECTION.on(:error) { |e| puts "Connection error (sandbox): #{e}" }
CONNECTION_POOL =
Apnotic::ConnectionPool.new(
CONNECTION_OPTIONS.dup,
size: ENV['CONNECTION_POOL_SIZE'].to_i
) do |connection|
connection.on(:error) { |e| puts "Connection error: #{e}" }
end
get '/' do
200
end
post '/push/:device_token/:id' do
request.body.rewind
notification = Apnotic::Notification.new(params[:device_token])
notification.topic = ENV['TOPIC']
notification.alert = { 'loc-key' => 'apns-default-message' }
notification.mutable_content = true
notification.custom_payload = {
i: params[:id],
m: Base64.urlsafe_encode64(request.body.read),
s: request.env['HTTP_ENCRYPTION'].split('salt=').last,
k: request.env['HTTP_CRYPTO_KEY'].split('dh=').last.split(';').first
}
if params[:sandbox] == 'true'
push = SANDBOX_CONNECTION.prepare_push(notification)
push.on(:response) { |r| puts "Bad response (sandbox): #{r.inspect}" unless r.ok? }
SANDBOX_CONNECTION.push_async(push)
else
CONNECTION_POOL.with do |connection|
push = connection.prepare_push(notification)
push.on(:response) { |r| puts "Bad response: #{r.inspect}" unless r.ok? }
connection.push_async(push)
end
end
202
end