feat: cleanup the fcm body part

This commit is contained in:
Muhsin Keloth
2024-06-05 13:13:21 +05:30
parent 61eaa96b85
commit 343f7ed2d3
3 changed files with 41 additions and 24 deletions
+1 -1
View File
@@ -76,4 +76,4 @@ yarn-debug.log*
.yarn-integrity
/storybook-static
config/chatwoot-3b1a1-c9a7423d7411.json
config/chatwoot-fcm-service-account-file.json
+2 -2
View File
@@ -19,7 +19,7 @@ class FCMService
end
def token_expired?
Time.now >= @token_info[:expires_at]
Time.zone.now >= @token_info[:expires_at]
end
def generate_token
@@ -30,7 +30,7 @@ class FCMService
token = authorizer.fetch_access_token!
{
token: token['access_token'],
expires_at: Time.now + token['expires_in'].to_i
expires_at: Time.zone.now + token['expires_in'].to_i
}
end
end
@@ -79,26 +79,10 @@ class Notification::PushNotificationService
fcm = fcm_service.fcm_client
message = {
'token': subscription.subscription_attributes['push_token'],
'data': {
payload: {
data: {
id: 1
}
}.to_json
},
'notification': {
title: notification.push_message_title,
body: notification.push_message_body
},
'android': {},
'apns': {
payload: {
aps: {
sound: 'default',
category: "#{Time.zone.now.to_i}"
}
}
},
'data': fcm_data,
'notification': fcm_notification,
'android': fcm_android_options,
'apns': fcm_apns_options,
'fcm_options': {
analytics_label: 'Label'
}
@@ -107,7 +91,6 @@ class Notification::PushNotificationService
remove_subscription_if_error(subscription, response)
end
def send_push_via_chatwoot_hub(subscription)
return if ENV['FCM_SERVER_KEY']
return unless ActiveModel::Type::Boolean.new.cast(ENV.fetch('ENABLE_PUSH_RELAY_SERVER', true))
@@ -132,4 +115,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