Chore: FCM Notification Improvements (#957)

Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-06-15 13:36:56 +05:30
committed by GitHub
parent 667e3abbe1
commit b0bbd757b5
6 changed files with 71 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
protect_from_forgery with: :null_session
before_action :fetch_notification, only: [:update]
before_action :set_primary_actor, only: [:read_all]
def index
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
@@ -9,7 +10,13 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
end
def read_all
current_user.notifications.where(account_id: current_account.id, read_at: nil).update(read_at: DateTime.now.utc)
if @primary_actor
current_user.notifications.where(account_id: current_account.id, primary_actor: @primary_actor, read_at: nil)
.update(read_at: DateTime.now.utc)
else
current_user.notifications.where(account_id: current_account.id, read_at: nil).update(read_at: DateTime.now.utc)
end
head :ok
end
@@ -20,6 +27,13 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
private
def set_primary_actor
return unless params[:primary_actor_type]
return unless Notification::PRIMARY_ACTORS.include?(params[:primary_actor_type])
@primary_actor = params[:primary_actor_type].safe_constantize.find_by(id: params[:primary_actor_id])
end
def fetch_notification
@notification = current_user.notifications.find(params[:id])
end

View File

@@ -37,11 +37,16 @@ class Notification < ApplicationRecord
enum notification_type: NOTIFICATION_TYPES
after_create_commit :process_notification_delivery
default_scope { order(id: :desc) }
PRIMARY_ACTORS = ['Conversation'].freeze
def push_event_data
{
id: id,
notification_type: notification_type,
primary_actor_type: primary_actor_type,
primary_actor_id: primary_actor_id,
primary_actor: primary_actor&.push_event_data,
read_at: read_at,
secondary_actor: secondary_actor&.push_event_data,

View File

@@ -66,15 +66,18 @@ class Notification::PushNotificationService
return unless subscription.fcm?
fcm = FCM.new(ENV['FCM_SERVER_KEY'])
options = {
response = fcm.send([subscription.subscription_attributes['push_token']], fcm_options)
subscription.destroy! if JSON.parse(response[:body])['results']&.first&.keys&.include?('error')
end
def fcm_options
{
"notification": {
"title": notification.notification_type.titleize,
"body": notification.push_message_title
},
"data": { notification: notification.push_event_data.to_json }
"data": { notification: notification.push_event_data.to_json },
"collapse_key": "chatwoot_#{notification.primary_actor_type.downcase}_#{notification.primary_actor_id}"
}
response = fcm.send([subscription.subscription_attributes['push_token']], options)
subscription.destroy! if JSON.parse(response[:body])['results']&.first&.keys&.include?('error')
end
end

View File

@@ -8,6 +8,8 @@ json.data do
json.id notification.id
json.notification_type notification.notification_type
json.push_message_title notification.push_message_title
json.primary_actor_type notification.primary_actor_type
json.primary_actor_id notification.primary_actor_id
json.primary_actor notification.primary_actor&.push_event_data
json.read_at notification.read_at
json.secondary_actor notification.secondary_actor&.push_event_data