jera_push 2.0.0 → 2.1.2

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: f7236749dd21e8caed1678a8e44c9e00b6e034e1198b9049bf5bc36ba5cee605
4
+ data.tar.gz: 36f20664f005ad3459f6651ad32ac1094bd1fa9556d198a332005c92363f6138
5
5
  SHA512:
6
- metadata.gz: d5494f51637a7f208dd28e7bbad74937b9ce9ccbea88c7f75264a9e1eda79bd3b69197daf5be9f12f5259f0a5582b5a28eb62efbccda64071004aee63602ebe2
7
- data.tar.gz: 82a894de332a1d3d6af5c95c388a74cf0f937c087d79a2df507322afbb5ea0ed96116ab93f86e4737229d4229595041eca99093ee5283b683f03bd760b40873c
6
+ metadata.gz: 03e288ec7ce98901ab70ac41a64df9f607117af92f954357cc383191abe4a970da61764367b324d97fad57c95805fc32a4fc0833ed8980a54bf550acd7418e7c
7
+ data.tar.gz: f33ffb817dc6f3f3491415703559cbeff5a3efc3e3f1aebab38f49b818605befe837d3a52538ec0f5c9ea64c4b8b3e8b7a11f0cb2dcb063bae049441045f14c9
@@ -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
@@ -4,7 +4,7 @@ class CreateJeraPushMessagesDevices < ActiveRecord::Migration<%= migration_versi
4
4
  t.integer :device_id, index: true
5
5
  t.integer :message_id, index: true
6
6
  t.string :status
7
- t.string :error_message
7
+ t.text :error_message
8
8
  t.string :firebase_id
9
9
 
10
10
  t.timestamps
@@ -1,10 +1,10 @@
1
1
  #this is the intilizer
2
2
  #here you will set up the jera push configuration
3
3
  JeraPush.setup do |config|
4
- config.firebase_api_key = "YOUR_API_KEY"
5
4
  # Change this for every new model
6
5
  config.resources_name = ["<%= file_name.classify %>"]
7
- config.project_name = "YOUR_PROJECT_NAME"
6
+ # You need to create a Account Service on google cloud, with editor permission or administrator. Flow this doc: https://cloud.google.com/iam/docs/service-accounts-create?hl=pt-br
7
+ config.project_id = "YOUR_PROJECT_ID" # inside of the account service json
8
8
  config.credentials_path = "YOUR_CREDENTIALS_PATH" #https://firebase.google.com/docs/cloud-messaging/migrate-v1?hl=pt-br#provide-credentials-manually
9
9
  ######################################################
10
10
  # Resource attribute showed in views #
@@ -3,64 +3,26 @@ 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
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 })
18
- end
19
-
20
- def add_device_to_topic(topic:, device:)
21
- send(url: "#{FIREBASE_INSTANCE_ID_URL}/v1/#{device.token}/rel/topics/#{topic}")
22
- end
23
-
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
- )
13
+ @client.authorization = fetch_access_token
32
14
  end
33
15
 
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
- )
16
+ def send_to_device(push:)
17
+ @client.send_message("projects/#{::JeraPush.project_id}", push.to_json, options: { retries: 3, multiplier: 1, max_interval: 2 })
44
18
  end
45
19
 
46
20
  private
47
21
 
48
- def send(url:, body: {})
49
- response = HTTParty.post(url, { body: body, headers: default_headers })
50
- JSON.parse(response)
51
- end
52
-
53
22
  def fetch_access_token
54
23
  @authorizer.fetch_access_token! if @authorizer.needs_access_token?
55
-
56
- @authorizer
57
- end
58
24
 
59
- def default_headers
60
- {
61
- "Authorization" => "key=#{::JeraPush.firebase_api_key}",
62
- "Content-Type" => "application/json"
63
- }
25
+ @authorizer
64
26
  end
65
27
  end
66
28
  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
@@ -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.gsub(/\s/, '_').gsub(/[^a-zA-Z0-9.~%_]/, '').slice(0, 50).downcase
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.2"
3
3
  end
data/lib/jera_push.rb CHANGED
@@ -22,12 +22,13 @@ 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
- mattr_accessor :firebase_api_key
27
- @@firebase_api_key = nil
28
-
29
- mattr_accessor :project_name
30
- @@project_name = nil
30
+ mattr_accessor :project_id
31
+ @@project_id = nil
31
32
 
32
33
  mattr_accessor :project_id
33
34
  @@project_id = nil
@@ -37,7 +38,7 @@ module JeraPush
37
38
 
38
39
  mattr_accessor :resources_name
39
40
  @@resources_name = nil
40
-
41
+
41
42
  mattr_accessor :credentials_path
42
43
  @@credentials_path = nil
43
44
 
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.2
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-07-16 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