jera_push 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f808be96eee74520b6a4907d7147a1cc4881cacb9a01a2d12a08ad81a2003d8f
4
- data.tar.gz: 301f267535f6edc5f5f614f829c11e5fcbeb49e6541ac9a23e6ec5c230c4778b
3
+ metadata.gz: aed822e33bf25c1e59e030e3d32d4aa45de406535a3993339ae12e43cd27b90c
4
+ data.tar.gz: d11416aeea95d06bbaa598a0d3c3c2c61c7912c0361f4e2888c378de9cf7add9
5
5
  SHA512:
6
- metadata.gz: d5494f51637a7f208dd28e7bbad74937b9ce9ccbea88c7f75264a9e1eda79bd3b69197daf5be9f12f5259f0a5582b5a28eb62efbccda64071004aee63602ebe2
7
- data.tar.gz: 82a894de332a1d3d6af5c95c388a74cf0f937c087d79a2df507322afbb5ea0ed96116ab93f86e4737229d4229595041eca99093ee5283b683f03bd760b40873c
6
+ metadata.gz: 793828acf069f18159e6ec301327b0cbe21e4e8600c1485931e26b0229e299d7c287e8b275aa0e7e5ca254fab8e5317f5d618f703eea78cc01d8572ebb482e91
7
+ data.tar.gz: 8bd496cc69b14561a847e5ae7ce1b198a9d21a98642abec25c11e46863be75b1e8ae5af357c04ec24728d0050178f3f346eeeb7ae553624271cbb7ae5d736393
@@ -43,7 +43,13 @@ module JeraPush
43
43
  def resend_push
44
44
  message = JeraPush::Message.find(params[:id])
45
45
  message.devices.find_each do |device|
46
- JeraPush::Services::SendPushService.new(device: 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,
47
53
  message: message,
48
54
  message_device: message.message_devices.where(device: device).last
49
55
  ).call
@@ -65,4 +71,4 @@ module JeraPush
65
71
  end
66
72
 
67
73
  end
68
- end
74
+ end
@@ -1,7 +1,5 @@
1
1
  module JeraPush
2
2
  class JeraPushController < ::ApplicationController
3
- protect_from_forgery unless: -> { request.format.json? }
4
-
5
3
  protected
6
4
 
7
5
  def render_invalid_params
@@ -3,18 +3,18 @@ module JeraPush::Firebase
3
3
  FIREBASE_API_VERSION = 'v1'.freeze
4
4
  FIREBASE_INSTANCE_ID_URL = 'https://iid.googleapis.com/iid'.freeze
5
5
  SCOPE = 'https://www.googleapis.com/auth/firebase.messaging'.freeze
6
-
6
+
7
7
  def initialize
8
8
  @client = Google::Apis::FcmV1::FirebaseCloudMessagingService.new
9
9
  @authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
10
10
  json_key_io: File.open(::JeraPush::credentials_path),
11
11
  scope: SCOPE
12
12
  )
13
- @client.authorization = fetch_access_token
13
+ @client.authorization = fetch_access_token
14
14
  end
15
-
16
- def send_to_device(message:)
17
- @client.send_message("projects/#{::JeraPush.project_name}", message, options: { retries: 3, multiplier: 1, max_interval: 2 })
15
+
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
18
  end
19
19
 
20
20
  def add_device_to_topic(topic:, device:)
@@ -52,9 +52,9 @@ module JeraPush::Firebase
52
52
 
53
53
  def fetch_access_token
54
54
  @authorizer.fetch_access_token! if @authorizer.needs_access_token?
55
-
55
+
56
56
  @authorizer
57
- end
57
+ end
58
58
 
59
59
  def default_headers
60
60
  {
@@ -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
@@ -0,0 +1,25 @@
1
+ module JeraPush
2
+ class Notification
3
+ attr_accessor :title
4
+ attr_accessor :body
5
+ attr_accessor :image
6
+
7
+ def initialize(
8
+ title: '',
9
+ body: '',
10
+ image: ''
11
+ )
12
+ self.title = title
13
+ self.body = body
14
+ self.image = image
15
+ end
16
+
17
+ def to_json
18
+ {
19
+ title: title,
20
+ body: body,
21
+ image: image
22
+ }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,90 @@
1
+ module JeraPush
2
+ class PushBody
3
+ attr_accessor :device
4
+ attr_accessor :devices
5
+ attr_accessor :notification
6
+ attr_accessor :data
7
+ attr_accessor :android
8
+ attr_accessor :ios
9
+ attr_accessor :analytics_label
10
+ attr_accessor :validate_only
11
+
12
+ def initialize(
13
+ data: {},
14
+ device: nil,
15
+ devices: [],
16
+ validate_only: false,
17
+ analytics_label: '',
18
+ ios_config: AppleConfig.new,
19
+ title: '',
20
+ body: '',
21
+ image: '',
22
+ notification: nil,
23
+ android_config: AndroidConfig.new
24
+ )
25
+ self.data = data
26
+ self.device = device
27
+ self.devices = devices
28
+ self.validate_only = validate_only
29
+ self.analytics_label = analytics_label
30
+ self.ios = ios_config
31
+ self.notification = notification.nil? ? Notification.new(title: title, body: body, image: image) : notification
32
+ self.android = android_config
33
+ end
34
+
35
+ def to_json
36
+ base_body
37
+ end
38
+
39
+ private
40
+
41
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#resource:-message
42
+ def base_body
43
+ {
44
+ message: build_message_body,
45
+ validate_only: validate_only
46
+ }
47
+ end
48
+
49
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#resource:-message
50
+ def build_message_body
51
+ {
52
+ token: device.token,
53
+ notification: notification_body,
54
+ data: data_body,
55
+ apns: apns_body,
56
+ android: android_body,
57
+ fcm_options: fcm_body
58
+ }
59
+ end
60
+
61
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#Notification
62
+ def notification_body
63
+ notification.to_json
64
+ end
65
+
66
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#resource:-message
67
+ def data_body
68
+ data.merge(notification.to_json)
69
+ end
70
+
71
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#ApnsConfig
72
+ def apns_body
73
+ ios.analytics_label = analytics_label
74
+ ios.to_json
75
+ end
76
+
77
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#AndroidConfig
78
+ def android_body
79
+ android.analytics_label = analytics_label
80
+ android.to_json
81
+ end
82
+
83
+ # REF: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br#FcmOptions
84
+ def fcm_body
85
+ {
86
+ analytics_label: analytics_label
87
+ }
88
+ end
89
+ end
90
+ end
@@ -18,11 +18,13 @@ module JeraPush
18
18
  # JeraPush::Message.send_broadcast(content: message_content)
19
19
  when :specific
20
20
  target_devices = JeraPush::Device.where(id: @devices.uniq.map(&:to_i))
21
- result = JeraPush::Services::SendToDevicesService.new(devices: target_devices,
22
- title: message_content[:title],
23
- body: message_content[:body],
24
- data: message_content
25
- ).call
21
+ push = JeraPush::PushBody.new(
22
+ title: message_content[:title],
23
+ body: message_content[:body],
24
+ data: message_content,
25
+ devices: target_devices
26
+ )
27
+ result = JeraPush::Services::SendToDevicesService.new(push: push).call
26
28
  result
27
29
  end
28
30
  end
@@ -42,4 +44,4 @@ module JeraPush
42
44
 
43
45
  end
44
46
  end
45
- end
47
+ end
@@ -1,12 +1,10 @@
1
1
  module JeraPush::Services
2
2
  class SendPushService < JeraPush::Services::BaseService
3
- def initialize(device:, message:, message_device:, android_config: {}, ios_config: {})
3
+ def initialize(push:, message:, message_device:)
4
4
  super
5
- @device = device
5
+ @push = push
6
6
  @message = message
7
7
  @message_device = message_device
8
- @android_config = android_config
9
- @ios_config = ios_config
10
8
  end
11
9
 
12
10
  def call
@@ -20,80 +18,25 @@ module JeraPush::Services
20
18
  private
21
19
 
22
20
  def send_push
23
- @response = @firebase.send_to_device(message: message_params(@device))
24
- @message_device.update(status: :success)
25
- @message.update(success_count: @message.success_count + 1)
21
+ @response = @firebase.send_to_device(push: @push)
22
+ save_success
26
23
  rescue Google::Apis::AuthorizationError => e
27
24
  message = JSON.parse(e.body)
28
- @message_device.update(status: :error, error_message: message.to_json)
29
- @message.update(failure_count: @message.failure_count + 1)
25
+ save_error(message)
30
26
  rescue Google::Apis::ClientError => e
31
27
  response = JSON.parse(e.body, symbolize_names: true)
32
28
  error_code = response[:error][:details][0][:errorCode]
33
- @message_device.update(status: :error, error_message: response.to_json)
34
- @message.update(failure_count: @message.failure_count + 1)
35
- end
36
-
37
- # Documentação com o atributos que podem ser usados em { message: }
38
- # https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=pt-br
39
- def message_params(device)
40
- case device.platform.to_sym
41
- when :android
42
- android_params(device.token)
43
- when :ios
44
- ios_params(device.token)
45
- else
46
- {}
47
- end
48
- end
49
-
50
- def android_params(token)
51
- {
52
- message: {
53
- token: token,
54
- notification: notification,
55
- data: @message.data,
56
- android: android_configs
57
- },
58
- validate_only: false
59
- }
60
- end
61
-
62
- def ios_params(token)
63
- {
64
- message: {
65
- token: token,
66
- notification: notification,
67
- data: @message.data,
68
- apns: apns_params
69
- },
70
- validate_only: false
71
- }
72
- end
73
-
74
- def notification
75
- { title: @message.title, body: @message.body }
76
- end
77
-
78
- def apns_params
79
- @ios_config.empty? ? default_apns_params : @ios_config
29
+ save_error(response)
80
30
  end
81
31
 
82
- def android_configs
83
- @android_config.merge({ priority: 'high'})
32
+ def save_success
33
+ @message_device.update(status: :success)
34
+ @message.update(success_count: @message.success_count + 1)
84
35
  end
85
36
 
86
- def default_apns_params
87
- {
88
- headers: {
89
- 'apns-priority': '5'
90
- },
91
- payload: {
92
- aps: {
93
- 'content-available': 1
94
- }
95
- }
96
- }
37
+ def save_error(error)
38
+ @message_device.update(status: :error, error_message: error.to_json)
39
+ @message.update(failure_count: @message.failure_count + 1)
97
40
  end
98
41
  end
99
42
  end
@@ -1,13 +1,9 @@
1
1
  module JeraPush::Services
2
2
  class SendToDeviceService < JeraPush::Services::BaseService
3
- def initialize(device:, title:, body:, data:, android: {}, ios: {})
3
+ def initialize(push:)
4
4
  super
5
- @device = device
6
- @title = title
7
- @body = body
8
- @data = data
9
- @android_config = android
10
- @ios_config = ios
5
+ @push = push
6
+ @device = @push.device
11
7
  end
12
8
 
13
9
  def call
@@ -29,7 +25,7 @@ module JeraPush::Services
29
25
  end
30
26
 
31
27
  def create_message
32
- @message = JeraPush::Message.create(title: @title, body: @body, data: data_params)
28
+ @message = JeraPush::Message.create(title: @push.notification.title, body: @push.notification.body, data: @push.data)
33
29
  end
34
30
 
35
31
  def add_devices_to_message
@@ -37,16 +33,10 @@ module JeraPush::Services
37
33
  end
38
34
 
39
35
  def send_push
40
- JeraPush::Services::SendPushService.new(device: @device,
41
- message: @message,
42
- message_device: @message_device,
43
- ios_config: @ios_config,
44
- android_config: @android_config
36
+ JeraPush::Services::SendPushService.new(push: @push,
37
+ message: @message,
38
+ message_device: @message_device
45
39
  ).call
46
40
  end
47
-
48
- def data_params
49
- @data.merge({ title: @title, body: @body })
50
- end
51
41
  end
52
42
  end
@@ -1,13 +1,9 @@
1
1
  module JeraPush::Services
2
2
  class SendToDevicesService < JeraPush::Services::BaseService
3
- def initialize(devices:, title:, body:, data:, android: {}, ios: {})
3
+ def initialize(push:)
4
4
  super
5
- @devices = devices
6
- @title = title
7
- @body = body
8
- @data = data
9
- @android_config = android
10
- @ios_config = ios
5
+ @push = push
6
+ @device = @push.devices
11
7
  end
12
8
 
13
9
  def call
@@ -28,27 +24,22 @@ module JeraPush::Services
28
24
  end
29
25
 
30
26
  def create_message
31
- @message = JeraPush::Message.create(title: @title, body: @body, data: data_params)
27
+ @message = JeraPush::Message.create(title: @push.notification.title, body: @push.notification.body, data: @push.data)
32
28
  end
33
29
 
34
30
  def send_push_to_devices
35
31
  @devices.each do |device|
36
32
  message_device = @message.message_devices.create(device: device)
33
+ @push.device = device
37
34
  send_push(device, message_device)
38
35
  end
39
36
  end
40
37
 
41
- def send_push(device, message_device)
42
- JeraPush::Services::SendPushService.new(device: device,
43
- message: @message,
44
- message_device: message_device,
45
- ios_config: @ios_config,
46
- android_config: @android_config
38
+ def send_push
39
+ JeraPush::Services::SendPushService.new(push: @push,
40
+ message: @message,
41
+ message_device: @message_device
47
42
  ).call
48
43
  end
49
-
50
- def data_params
51
- @data.merge({ title: @title, body: @body })
52
- end
53
44
  end
54
45
  end
@@ -1,3 +1,3 @@
1
1
  module JeraPush
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
data/lib/jera_push.rb CHANGED
@@ -22,6 +22,10 @@ module JeraPush
22
22
  autoload :Message, 'jera_push/models/message.rb'
23
23
  autoload :MessageDevice, 'jera_push/models/message_device.rb'
24
24
  autoload :DeviceFilter, 'jera_push/models/device_filter.rb'
25
+ autoload :PushBody, 'jera_push/models/push_body.rb'
26
+ autoload :AppleConfig, 'jera_push/models/apple_config.rb'
27
+ autoload :AndroidConfig, 'jera_push/models/android_config.rb'
28
+ autoload :Notification, 'jera_push/models/notification.rb'
25
29
 
26
30
  mattr_accessor :firebase_api_key
27
31
  @@firebase_api_key = nil
@@ -37,7 +41,7 @@ module JeraPush
37
41
 
38
42
  mattr_accessor :resources_name
39
43
  @@resources_name = nil
40
-
44
+
41
45
  mattr_accessor :credentials_path
42
46
  @@credentials_path = nil
43
47
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jera_push
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-16 00:00:00.000000000 Z
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -289,10 +289,14 @@ files:
289
289
  - lib/jera_push/engine.rb
290
290
  - lib/jera_push/firebase/api_result.rb
291
291
  - lib/jera_push/firebase/client.rb
292
+ - lib/jera_push/models/android_config.rb
293
+ - lib/jera_push/models/apple_config.rb
292
294
  - lib/jera_push/models/device.rb
293
295
  - lib/jera_push/models/device_filter.rb
294
296
  - lib/jera_push/models/message.rb
295
297
  - lib/jera_push/models/message_device.rb
298
+ - lib/jera_push/models/notification.rb
299
+ - lib/jera_push/models/push_body.rb
296
300
  - lib/jera_push/services/base_service.rb
297
301
  - lib/jera_push/services/send_message.rb
298
302
  - lib/jera_push/services/send_push_service.rb