jera_push 1.2.4 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +12 -18
- data/app/controllers/jera_push/admin/messages_controller.rb +27 -12
- data/app/controllers/jera_push/jera_push_controller.rb +0 -2
- data/app/presenters/jera_push/message_presenter.rb +22 -18
- data/app/views/jera_push/admin/messages/show.html.erb +7 -4
- data/config/locale/jera_push.pt-BR.yml +7 -0
- data/config/routes.rb +1 -0
- data/lib/generators/active_record/templates/create_jera_push_devices.rb +2 -1
- data/lib/generators/active_record/templates/create_jera_push_messages.rb +6 -3
- data/lib/generators/active_record/templates/create_jera_push_messages_devices.rb +3 -3
- data/lib/generators/jera_push/templates/jera_push.rb +2 -1
- data/lib/jera_push/firebase/client.rb +53 -56
- data/lib/jera_push/models/android_config.rb +33 -0
- data/lib/jera_push/models/apple_config.rb +61 -0
- data/lib/jera_push/models/device.rb +3 -31
- data/lib/jera_push/models/message.rb +2 -142
- data/lib/jera_push/models/message_device.rb +1 -0
- data/lib/jera_push/models/notification.rb +25 -0
- data/lib/jera_push/models/push_body.rb +90 -0
- data/lib/jera_push/services/base_service.rb +23 -0
- data/lib/jera_push/services/send_message.rb +20 -4
- data/lib/jera_push/services/send_push_service.rb +42 -0
- data/lib/jera_push/services/send_to_device_service.rb +42 -0
- data/lib/jera_push/services/send_to_devices_service.rb +45 -0
- data/lib/jera_push/services/send_to_everyone_service.rb +40 -0
- data/lib/jera_push/services/send_to_topic_service.rb +37 -0
- data/lib/jera_push/services/topic_service.rb +18 -0
- data/lib/jera_push/version.rb +1 -1
- data/lib/jera_push.rb +23 -5
- metadata +75 -116
- data/test/dummy/README.rdoc +0 -28
- data/test/dummy/Rakefile +0 -6
- data/test/dummy/app/assets/javascripts/application.js +0 -13
- data/test/dummy/app/assets/stylesheets/application.css +0 -15
- data/test/dummy/app/controllers/application_controller.rb +0 -5
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/models/user.rb +0 -3
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/bin/bundle +0 -3
- data/test/dummy/bin/rails +0 -4
- data/test/dummy/bin/rake +0 -4
- data/test/dummy/bin/setup +0 -29
- data/test/dummy/config/application.rb +0 -25
- data/test/dummy/config/boot.rb +0 -5
- data/test/dummy/config/database.yml +0 -16
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -41
- data/test/dummy/config/environments/production.rb +0 -79
- data/test/dummy/config/environments/test.rb +0 -42
- data/test/dummy/config/initializers/assets.rb +0 -12
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/test/dummy/config/initializers/inflections.rb +0 -16
- data/test/dummy/config/initializers/mime_types.rb +0 -4
- data/test/dummy/config/initializers/session_store.rb +0 -3
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -23
- data/test/dummy/config/locales/pt-BR.yml +0 -23
- data/test/dummy/config/routes.rb +0 -57
- data/test/dummy/config/secrets.yml +0 -22
- data/test/dummy/config.ru +0 -4
- data/test/dummy/db/migrate/20161005121035_create_users.rb +0 -10
- data/test/dummy/db/schema.rb +0 -61
- data/test/dummy/db/seeds.rb +0 -70
- data/test/dummy/public/404.html +0 -67
- data/test/dummy/public/422.html +0 -67
- data/test/dummy/public/500.html +0 -66
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/fixtures/users.yml +0 -7
- data/test/dummy/test/models/user_test.rb +0 -7
- data/test/jera_push_test.rb +0 -7
- data/test/test_helper.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aed822e33bf25c1e59e030e3d32d4aa45de406535a3993339ae12e43cd27b90c
|
4
|
+
data.tar.gz: d11416aeea95d06bbaa598a0d3c3c2c61c7912c0361f4e2888c378de9cf7add9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 793828acf069f18159e6ec301327b0cbe21e4e8600c1485931e26b0229e299d7c287e8b275aa0e7e5ca254fab8e5317f5d618f703eea78cc01d8572ebb482e91
|
7
|
+
data.tar.gz: 8bd496cc69b14561a847e5ae7ce1b198a9d21a98642abec25c11e46863be75b1e8ae5af357c04ec24728d0050178f3f346eeeb7ae553624271cbb7ae5d736393
|
data/Rakefile
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
1
3
|
begin
|
2
4
|
require 'bundler/setup'
|
5
|
+
|
6
|
+
Bundler::GemHelper.install_tasks
|
3
7
|
rescue LoadError
|
4
|
-
puts '
|
8
|
+
puts 'although not required, bundler is recommended for running the tests'
|
5
9
|
end
|
6
10
|
|
7
|
-
|
11
|
+
task default: :spec
|
12
|
+
|
13
|
+
require 'rspec/core/rake_task'
|
8
14
|
|
9
15
|
RDoc::Task.new(:rdoc) do |rdoc|
|
10
16
|
rdoc.rdoc_dir = 'rdoc'
|
@@ -14,21 +20,9 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
20
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
21
|
end
|
16
22
|
|
23
|
+
require 'rubocop/rake_task'
|
17
24
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
Bundler::GemHelper.install_tasks
|
23
|
-
|
24
|
-
require 'rake/testtask'
|
25
|
-
|
26
|
-
Rake::TestTask.new(:test) do |t|
|
27
|
-
t.libs << 'lib'
|
28
|
-
t.libs << 'test'
|
29
|
-
t.pattern = 'test/**/*_test.rb'
|
30
|
-
t.verbose = false
|
25
|
+
RuboCop::RakeTask.new do |task|
|
26
|
+
task.requires << 'rubocop-performance'
|
27
|
+
task.requires << 'rubocop-rspec'
|
31
28
|
end
|
32
|
-
|
33
|
-
|
34
|
-
task default: :test
|
@@ -28,7 +28,7 @@ module JeraPush
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def create
|
31
|
-
push_message = JeraPush::
|
31
|
+
push_message = JeraPush::Services::SendMessage.new(params).call
|
32
32
|
|
33
33
|
if push_message
|
34
34
|
flash[:notice] = t('jera_push.admin.messages.new.toast.success')
|
@@ -40,20 +40,35 @@ module JeraPush
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
def resend_push
|
44
|
+
message = JeraPush::Message.find(params[:id])
|
45
|
+
message.devices.find_each do |device|
|
46
|
+
push = JeraPush::PushBody.new(
|
47
|
+
title: message.title,
|
48
|
+
body: message.body,
|
49
|
+
data: message.data,
|
50
|
+
device: device
|
51
|
+
)
|
52
|
+
JeraPush::Services::SendPushService.new(push: push,
|
53
|
+
message: message,
|
54
|
+
message_device: message.message_devices.where(device: device).last
|
55
|
+
).call
|
56
|
+
end
|
57
|
+
flash[:notice] = t('jera_push.admin.messages.new.toast.success')
|
58
|
+
redirect_to jera_push_admin_message_path(message)
|
59
|
+
end
|
60
|
+
|
43
61
|
private
|
44
62
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
63
|
+
def apply_filter
|
64
|
+
@filter = JeraPush::DeviceFilter.new device_filter_params
|
65
|
+
@devices = @filter.search.limit(params[:limit]).order(created_at: :desc)
|
66
|
+
@message_devices = JeraPush::MessageDevice.includes(:device).where(message_id: params[:id], device_id: @devices.pluck(:id))
|
67
|
+
end
|
50
68
|
|
51
|
-
|
52
|
-
|
53
|
-
|
69
|
+
def device_filter_params
|
70
|
+
params.permit(:value, :field, platform: []).merge({ message_id: params[:id] })
|
71
|
+
end
|
54
72
|
|
55
|
-
def device_filter_params
|
56
|
-
params.permit(:value, :field, :resource_name, platform: []).merge({ message_id: params[:id] })
|
57
|
-
end
|
58
73
|
end
|
59
74
|
end
|
@@ -1,25 +1,29 @@
|
|
1
1
|
class JeraPush::MessagePresenter < BasePresenter
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
def list_content
|
4
|
+
JSON.pretty_generate({
|
5
|
+
title: item.title,
|
6
|
+
body: item.body
|
7
|
+
})
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def display_content
|
11
|
+
JSON.pretty_generate({
|
12
|
+
title: item.title,
|
13
|
+
body: item.body,
|
14
|
+
data: item.data
|
15
|
+
})
|
16
|
+
end
|
13
17
|
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
def display_created_at
|
19
|
+
item.created_at.strftime('%d/%m/%Y %H:%M')
|
20
|
+
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
def show_link
|
23
|
+
helpers.link_to(
|
24
|
+
helpers.t('jera_push.admin.buttons.details'),
|
25
|
+
Rails.application.routes.url_helpers.jera_push_admin_message_path(item), class: 'waves-effect waves-light btn'
|
26
|
+
)
|
27
|
+
end
|
24
28
|
|
25
29
|
end
|
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
<div class="details">
|
4
4
|
<h3><%= t('jera_push.active_record.model.message.one') %></h3>
|
5
|
-
|
5
|
+
<%= form_tag resend_push_jera_push_admin_message_path(@message), method: :get, class: 'col s12', remote: true do |f| %>
|
6
|
+
<%= submit_tag t('jera_push.admin.messages.resend'), class: 'btn' %>
|
7
|
+
<% end %>
|
8
|
+
<br>
|
6
9
|
<span class="field">
|
7
10
|
<%= t('jera_push.active_record.messages.attributes.id') %>
|
8
11
|
</span>
|
@@ -13,7 +16,7 @@
|
|
13
16
|
</span>
|
14
17
|
<blockquote class="json-message"><%= @message.display_content %></blockquote>
|
15
18
|
|
16
|
-
<% unless @message.kind.
|
19
|
+
<% unless @message.kind.everyone? %>
|
17
20
|
<span class="field">
|
18
21
|
<%= t('jera_push.active_record.messages.attributes.status') %>
|
19
22
|
</span>
|
@@ -25,8 +28,8 @@
|
|
25
28
|
<% end %>
|
26
29
|
</div>
|
27
30
|
|
28
|
-
<% if @message.kind.
|
29
|
-
<h3><%= JeraPush::Message.kind.
|
31
|
+
<% if @message.kind.everyone? %>
|
32
|
+
<h3><%= JeraPush::Message.kind.everyone.text %></h3>
|
30
33
|
|
31
34
|
<div class="message-info">
|
32
35
|
<%= render partial: 'jera_push/admin/messages/broadcast_message', message: @message %>
|
@@ -29,6 +29,7 @@ pt-BR:
|
|
29
29
|
admin:
|
30
30
|
attributes:
|
31
31
|
id: Identificador
|
32
|
+
token: Token
|
32
33
|
name: Nome
|
33
34
|
email: Email
|
34
35
|
navbar:
|
@@ -62,6 +63,7 @@ pt-BR:
|
|
62
63
|
messages:
|
63
64
|
failures: "Falhas: %{value}"
|
64
65
|
success: "Sucessos: %{value}"
|
66
|
+
resend: Reenviar a push
|
65
67
|
show:
|
66
68
|
broadcast:
|
67
69
|
platform: Plataforma
|
@@ -76,3 +78,8 @@ pt-BR:
|
|
76
78
|
new_attribute: Novo Atributo
|
77
79
|
specific: Específico
|
78
80
|
broadcast: Broadcast
|
81
|
+
services:
|
82
|
+
errors:
|
83
|
+
not_found:
|
84
|
+
device: Dispositivo não pode ficar em branco
|
85
|
+
|
data/config/routes.rb
CHANGED
@@ -5,8 +5,9 @@ class CreateJeraPushDevices < ActiveRecord::Migration<%= migration_version %>
|
|
5
5
|
t.string :platform, index: true
|
6
6
|
t.references :pushable, polymorphic: true, index: true
|
7
7
|
|
8
|
-
t.timestamps
|
8
|
+
t.timestamps
|
9
9
|
end
|
10
|
+
|
10
11
|
add_index :jera_push_devices, [:token, :platform], unique: true
|
11
12
|
end
|
12
13
|
end
|
@@ -1,17 +1,20 @@
|
|
1
1
|
class CreateJeraPushMessages < ActiveRecord::Migration<%= migration_version %>
|
2
2
|
def change
|
3
3
|
create_table :jera_push_messages do |t|
|
4
|
-
t.
|
4
|
+
t.string :title
|
5
|
+
t.text :body
|
6
|
+
t.json :data
|
7
|
+
t.string :multicast_id
|
5
8
|
t.text :broadcast_result
|
6
9
|
t.string :status
|
7
10
|
t.string :kind
|
8
11
|
t.string :topic
|
9
12
|
t.string :firebase_id
|
10
|
-
t.
|
13
|
+
t.json :error_message
|
11
14
|
t.integer :failure_count, default: 0
|
12
15
|
t.integer :success_count, default: 0
|
13
16
|
|
14
|
-
t.timestamps
|
17
|
+
t.timestamps
|
15
18
|
end
|
16
19
|
end
|
17
20
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
class CreateJeraPushMessagesDevices < ActiveRecord::Migration<%= migration_version %>
|
2
2
|
def change
|
3
3
|
create_table :jera_push_messages_devices do |t|
|
4
|
-
t.integer :device_id, index: true
|
5
|
-
t.integer :message_id, index: true
|
4
|
+
t.integer :device_id, index: true
|
5
|
+
t.integer :message_id, index: true
|
6
6
|
t.string :status
|
7
7
|
t.string :error_message
|
8
8
|
t.string :firebase_id
|
9
9
|
|
10
|
-
t.timestamps
|
10
|
+
t.timestamps
|
11
11
|
end
|
12
12
|
|
13
13
|
add_index :jera_push_messages_devices, [:device_id, :message_id], unique: true, name: :jera_push_index_messages_id_devices_id
|
@@ -4,7 +4,8 @@ JeraPush.setup do |config|
|
|
4
4
|
config.firebase_api_key = "YOUR_API_KEY"
|
5
5
|
# Change this for every new model
|
6
6
|
config.resources_name = ["<%= file_name.classify %>"]
|
7
|
-
|
7
|
+
config.project_name = "YOUR_PROJECT_NAME"
|
8
|
+
config.credentials_path = "YOUR_CREDENTIALS_PATH" #https://firebase.google.com/docs/cloud-messaging/migrate-v1?hl=pt-br#provide-credentials-manually
|
8
9
|
######################################################
|
9
10
|
# Resource attribute showed in views #
|
10
11
|
# IMPORTANT: All models need to have this attributes #
|
@@ -1,69 +1,66 @@
|
|
1
|
-
module JeraPush
|
2
|
-
|
3
|
-
|
1
|
+
module JeraPush::Firebase
|
2
|
+
class Client
|
3
|
+
FIREBASE_API_VERSION = 'v1'.freeze
|
4
|
+
FIREBASE_INSTANCE_ID_URL = 'https://iid.googleapis.com/iid'.freeze
|
5
|
+
SCOPE = 'https://www.googleapis.com/auth/firebase.messaging'.freeze
|
4
6
|
|
5
|
-
|
7
|
+
def initialize
|
8
|
+
@client = Google::Apis::FcmV1::FirebaseCloudMessagingService.new
|
9
|
+
@authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
|
10
|
+
json_key_io: File.open(::JeraPush::credentials_path),
|
11
|
+
scope: SCOPE
|
12
|
+
)
|
13
|
+
@client.authorization = fetch_access_token
|
14
|
+
end
|
6
15
|
|
7
|
-
|
8
|
-
|
16
|
+
def send_to_device(push:)
|
17
|
+
@client.send_message("projects/#{::JeraPush.project_name}", push.to_json, options: { retries: 3, multiplier: 1, max_interval: 2 })
|
18
|
+
end
|
9
19
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
"Content-Type" => "application/json"
|
14
|
-
}
|
15
|
-
end
|
20
|
+
def add_device_to_topic(topic:, device:)
|
21
|
+
send(url: "#{FIREBASE_INSTANCE_ID_URL}/v1/#{device.token}/rel/topics/#{topic}")
|
22
|
+
end
|
16
23
|
|
17
|
-
|
18
|
-
|
19
|
-
|
24
|
+
def remove_device_from_topic(topic:, devices: [])
|
25
|
+
send(
|
26
|
+
url: "#{FIREBASE_INSTANCE_ID_URL}/v1:batchRemove",
|
27
|
+
body: {
|
28
|
+
to: "/topics/#{topic}",
|
29
|
+
registration_tokens: devices.pluck(:token)
|
30
|
+
}.to_json
|
31
|
+
)
|
32
|
+
end
|
20
33
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
34
|
+
def send_message_to_topic(message:, topic:)
|
35
|
+
send(
|
36
|
+
url: FIREBASE_URL,
|
37
|
+
body: {
|
38
|
+
title: message.title,
|
39
|
+
body: message.body,
|
40
|
+
to: "/topics/#{topic}",
|
41
|
+
priority: 'high'
|
42
|
+
}.to_json
|
43
|
+
)
|
44
|
+
end
|
28
45
|
|
29
|
-
|
30
|
-
body = { to: "/topics/#{topic}", priority: 'high' }
|
31
|
-
response = HTTParty.post(FIREBASE_URL, { body: body.merge!(message).to_json, headers: default_headers })
|
32
|
-
puts response
|
33
|
-
ApiResult.new(response, topic: topic)
|
34
|
-
end
|
46
|
+
private
|
35
47
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
48
|
+
def send(url:, body: {})
|
49
|
+
response = HTTParty.post(url, { body: body, headers: default_headers })
|
50
|
+
JSON.parse(response)
|
51
|
+
end
|
41
52
|
|
42
|
-
|
43
|
-
|
44
|
-
response = HTTParty.post(url, { body: Hash.new.to_json, headers: default_headers })
|
45
|
-
ApiResult.new(response)
|
46
|
-
end
|
53
|
+
def fetch_access_token
|
54
|
+
@authorizer.fetch_access_token! if @authorizer.needs_access_token?
|
47
55
|
|
48
|
-
|
49
|
-
|
50
|
-
body = {
|
51
|
-
"to": "/topics/#{topic}",
|
52
|
-
"registration_tokens": devices.map(&:token),
|
53
|
-
}
|
54
|
-
response = HTTParty.post(url, { body: body.to_json, headers: default_headers })
|
55
|
-
ApiResult.new(response)
|
56
|
-
end
|
56
|
+
@authorizer
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
}
|
64
|
-
response = HTTParty.post(url, { body: body.to_json, headers: default_headers })
|
65
|
-
ApiResult.new(response)
|
66
|
-
end
|
59
|
+
def default_headers
|
60
|
+
{
|
61
|
+
"Authorization" => "key=#{::JeraPush.firebase_api_key}",
|
62
|
+
"Content-Type" => "application/json"
|
63
|
+
}
|
67
64
|
end
|
68
65
|
end
|
69
66
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module JeraPush
|
2
|
+
class AndroidConfig
|
3
|
+
# REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#AndroidMessagePriority
|
4
|
+
attr_accessor :priority
|
5
|
+
attr_accessor :analytics_label
|
6
|
+
|
7
|
+
|
8
|
+
def initialize(
|
9
|
+
priority: 'high',
|
10
|
+
analytics_label: nil
|
11
|
+
)
|
12
|
+
self.priority = priority
|
13
|
+
self.analytics_label = analytics_label
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_json
|
17
|
+
android_body
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#AndroidConfig
|
23
|
+
def android_body
|
24
|
+
{
|
25
|
+
priority: priority,
|
26
|
+
# REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#FcmOptions
|
27
|
+
fcm_options: {
|
28
|
+
analytics_label: analytics_label
|
29
|
+
}
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module JeraPush
|
2
|
+
# REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#ApnsConfig
|
3
|
+
class AppleConfig
|
4
|
+
# Specify 5 to send the notification based on power considerations on the user’s device.
|
5
|
+
# Specify 10 to send the notification immediately.
|
6
|
+
attr_accessor :apns_priority
|
7
|
+
attr_accessor :headers
|
8
|
+
# REF: https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification
|
9
|
+
# REF: https://developer.apple.com/documentation/usernotifications/pushing-background-updates-to-your-app
|
10
|
+
attr_accessor :content_available
|
11
|
+
# REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#ApnsFcmOptions
|
12
|
+
attr_accessor :analytics_label
|
13
|
+
attr_accessor :analytics_image
|
14
|
+
|
15
|
+
def initialize(
|
16
|
+
apns_priority: '5',
|
17
|
+
headers: {},
|
18
|
+
content_available: 1,
|
19
|
+
analytics_label: nil,
|
20
|
+
analytics_image: nil
|
21
|
+
)
|
22
|
+
self.apns_priority = apns_priority
|
23
|
+
self.headers = headers
|
24
|
+
self.content_available = content_available
|
25
|
+
self.analytics_label = analytics_label
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_json
|
29
|
+
apns_body
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def apns_body
|
35
|
+
{
|
36
|
+
headers: headers_body,
|
37
|
+
payload: payload_body,
|
38
|
+
fcm_options: fcm_body
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def headers_body
|
43
|
+
headers.merge!({ 'apns-priority': apns_priority} )
|
44
|
+
end
|
45
|
+
|
46
|
+
def payload_body
|
47
|
+
{
|
48
|
+
aps: {
|
49
|
+
'content-available': content_available
|
50
|
+
}
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def fcm_body
|
55
|
+
{
|
56
|
+
analytics_label: analytics_label,
|
57
|
+
image: analytics_image
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -3,21 +3,16 @@ require 'enumerize'
|
|
3
3
|
class JeraPush::Device < ActiveRecord::Base
|
4
4
|
extend Enumerize
|
5
5
|
|
6
|
-
DEFAULT_TOPIC = 'general'
|
7
|
-
|
8
6
|
self.table_name = "jera_push_devices"
|
9
7
|
|
10
|
-
has_many :message_devices, table_name: "jera_push_message_devices"
|
11
|
-
has_many :messages, through: :message_devices, table_name: "jera_push_messages"
|
12
|
-
|
13
8
|
belongs_to :pushable, polymorphic: true
|
14
9
|
|
10
|
+
has_many :message_devices, class_name: "JeraPush::MessageDevice"
|
11
|
+
has_many :messages, through: :message_devices, class_name: "JeraPush::Message"
|
12
|
+
|
15
13
|
validates :token, :platform, presence: true
|
16
14
|
validates :token, uniqueness: { scope: :platform }
|
17
15
|
|
18
|
-
after_create :register_to_current_topic
|
19
|
-
before_destroy :unregister_from_current_topic
|
20
|
-
|
21
16
|
enumerize :platform, in: [:android, :ios, :chrome], predicate: true
|
22
17
|
|
23
18
|
scope :ios, -> { where(platform: :ios) }
|
@@ -25,27 +20,4 @@ class JeraPush::Device < ActiveRecord::Base
|
|
25
20
|
scope :chrome, -> { where(platform: :chrome) }
|
26
21
|
scope :with_joins, -> (resource_type) { joins("INNER JOIN #{resource_type.downcase.pluralize} ON jera_push_devices.pushable_id = #{resource_type.downcase.pluralize}.id AND jera_push_devices.pushable_type = '#{resource_type}'") }
|
27
22
|
|
28
|
-
def send_message(message)
|
29
|
-
JeraPush::Message.send_to self, content: message
|
30
|
-
end
|
31
|
-
|
32
|
-
def subscribe(topic)
|
33
|
-
client = JeraPush::Firebase::Client.instance
|
34
|
-
client.add_device_to_topic(topic: topic, device: self)
|
35
|
-
end
|
36
|
-
|
37
|
-
def unsubscribe(topic)
|
38
|
-
client = JeraPush::Firebase::Client.instance
|
39
|
-
client.remove_device_from_topic(topic: topic, devices: [self])
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def register_to_current_topic
|
45
|
-
subscribe(JeraPush.send("topic_#{self.platform}"))
|
46
|
-
end
|
47
|
-
|
48
|
-
def unregister_from_current_topic
|
49
|
-
unsubscribe(JeraPush.send("topic_#{self.platform}"))
|
50
|
-
end
|
51
23
|
end
|