Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e622375b23 | ||
|
|
410eeeb610 | ||
|
|
343f7ed2d3 | ||
|
|
61eaa96b85 |
+2
-1
@@ -75,4 +75,5 @@ yalc.lock
|
||||
yarn-debug.log*
|
||||
.yarn-integrity
|
||||
|
||||
/storybook-static
|
||||
/storybook-static
|
||||
config/chatwoot-fcm-service-account-file.json
|
||||
@@ -40,7 +40,7 @@ class SuperAdmin::AppConfigsController < SuperAdmin::ApplicationController
|
||||
when 'email'
|
||||
['MAILER_INBOUND_EMAIL_DOMAIN']
|
||||
else
|
||||
%w[ENABLE_ACCOUNT_SIGNUP]
|
||||
%w[ENABLE_ACCOUNT_SIGNUP FIREBASE_PROJECT_ID FIREBASE_CREDENTIALS]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
class FCMService
|
||||
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'].freeze
|
||||
|
||||
def initialize(project_id, credentials)
|
||||
@project_id = project_id
|
||||
@credentials = credentials
|
||||
@token_info = nil
|
||||
end
|
||||
|
||||
def fcm_client
|
||||
FCM.new(current_token, credentials_path, @project_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_token
|
||||
@token_info = generate_token if @token_info.nil? || token_expired?
|
||||
@token_info[:token]
|
||||
end
|
||||
|
||||
def token_expired?
|
||||
Time.zone.now >= @token_info[:expires_at]
|
||||
end
|
||||
|
||||
def generate_token
|
||||
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
|
||||
json_key_io: File.open(credentials_path),
|
||||
scope: SCOPES
|
||||
)
|
||||
token = authorizer.fetch_access_token!
|
||||
{
|
||||
token: token['access_token'],
|
||||
expires_at: Time.zone.now + token['expires_in'].to_i
|
||||
}
|
||||
end
|
||||
|
||||
def credentials_path
|
||||
file = Tempfile.new(['firebase', '.json'])
|
||||
file.write(@credentials)
|
||||
file.rewind
|
||||
file.path
|
||||
ensure
|
||||
file.close
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,4 @@
|
||||
require_relative 'fcm_service'
|
||||
class Notification::PushNotificationService
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
@@ -70,11 +71,24 @@ class Notification::PushNotificationService
|
||||
end
|
||||
|
||||
def send_fcm_push(subscription)
|
||||
return unless ENV['FCM_SERVER_KEY']
|
||||
return unless GlobalConfigService.load('FIREBASE_PROJECT_ID', nil) && GlobalConfigService.load('FIREBASE_CREDENTIALS', nil)
|
||||
return unless subscription.fcm?
|
||||
|
||||
fcm = FCM.new(ENV.fetch('FCM_SERVER_KEY', nil))
|
||||
response = fcm.send([subscription.subscription_attributes['push_token']], fcm_options)
|
||||
fcm_service = FCMService.new(
|
||||
GlobalConfigService.load('FIREBASE_PROJECT_ID', nil), GlobalConfigService.load('FIREBASE_CREDENTIALS', nil)
|
||||
)
|
||||
fcm = fcm_service.fcm_client
|
||||
message = {
|
||||
'token': subscription.subscription_attributes['push_token'],
|
||||
'data': fcm_data,
|
||||
'notification': fcm_notification,
|
||||
'android': fcm_android_options,
|
||||
'apns': fcm_apns_options,
|
||||
'fcm_options': {
|
||||
analytics_label: 'Label'
|
||||
}
|
||||
}
|
||||
response = fcm.send_v1(message)
|
||||
remove_subscription_if_error(subscription, response)
|
||||
end
|
||||
|
||||
@@ -102,4 +116,38 @@ class Notification::PushNotificationService
|
||||
collapse_key: "chatwoot_#{notification.primary_actor_type.downcase}_#{notification.primary_actor_id}"
|
||||
}
|
||||
end
|
||||
|
||||
def fcm_data
|
||||
{
|
||||
payload: {
|
||||
data: {
|
||||
notification: notification.fcm_push_data
|
||||
}
|
||||
}.to_json
|
||||
}
|
||||
end
|
||||
|
||||
def fcm_notification
|
||||
{
|
||||
title: notification.push_message_title,
|
||||
body: notification.push_message_body
|
||||
}
|
||||
end
|
||||
|
||||
def fcm_android_options
|
||||
{
|
||||
priority: 'high'
|
||||
}
|
||||
end
|
||||
|
||||
def fcm_apns_options
|
||||
{
|
||||
payload: {
|
||||
aps: {
|
||||
sound: 'default',
|
||||
category: Time.zone.now.to_i.to_s
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -204,3 +204,16 @@
|
||||
locked: false
|
||||
description: 'Disable rendering profile update page for users'
|
||||
## ------ End of Configs added for enterprise clients ------ ##
|
||||
|
||||
## ------ Configs added for FCM v1 notifications ------ ##
|
||||
- name: FIREBASE_PROJECT_ID
|
||||
display_title: 'Firebase Project ID'
|
||||
value:
|
||||
locked: false
|
||||
description: 'Firebase project ID'
|
||||
- name: FIREBASE_CREDENTIALS
|
||||
display_title: 'Firebase Credentials'
|
||||
value:
|
||||
locked: false
|
||||
description: 'Contents on your firebase credentials json file'
|
||||
## ------ End of Configs added for FCM v1 notifications ------ ##
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
class ChatwootHub
|
||||
BASE_URL = ENV.fetch('CHATWOOT_HUB_URL', 'https://hub.2.chatwoot.com')
|
||||
BASE_URL = ENV.fetch('CHATWOOT_HUB_URL', 'http://localhost:3001')
|
||||
PING_URL = "#{BASE_URL}/ping".freeze
|
||||
REGISTRATION_URL = "#{BASE_URL}/instances".freeze
|
||||
PUSH_NOTIFICATION_URL = "#{BASE_URL}/send_push".freeze
|
||||
|
||||
Reference in New Issue
Block a user