Chore: FCM Push Fixes (#934)

This commit is contained in:
Sojan Jose
2020-06-09 17:42:18 +05:30
committed by GitHub
parent 40481f6462
commit 8b022311c0
6 changed files with 92 additions and 21 deletions

View File

@@ -4,8 +4,13 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
before_action :fetch_notification, only: [:update]
def index
@notifications = current_user.notifications.where(account_id: Current.account.id)
render json: @notifications
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
@notifications = current_user.notifications.where(account_id: current_account.id).page params[:page]
end
def read_all
current_user.notifications.where(account_id: current_account.id, read_at: nil).update(read_at: DateTime.now.utc)
head :ok
end
def update

View File

@@ -38,6 +38,29 @@ class Notification < ApplicationRecord
after_create_commit :process_notification_delivery
def push_event_data
{
id: id,
notification_type: notification_type,
primary_actor: primary_actor&.push_event_data,
read_at: read_at,
secondary_actor: secondary_actor&.push_event_data,
user: user&.push_event_data,
created_at: created_at
}
end
# TODO: move to a data presenter
def push_message_title
if notification_type == 'conversation_creation'
return "A new conversation [ID -#{primary_actor.display_id}] has been created in #{primary_actor.inbox.name}"
end
return "A new conversation [ID -#{primary_actor.display_id}] has been assigned to you." if notification_type == 'conversation_assignment'
''
end
private
def process_notification_delivery

View File

@@ -29,21 +29,9 @@ class Notification::PushNotificationService
@conversation ||= notification.primary_actor
end
def push_message_title
if notification.notification_type == 'conversation_creation'
return "A new conversation [ID -#{conversation.display_id}] has been created in #{conversation.inbox.name}"
end
if notification.notification_type == 'conversation_assignment'
return "A new conversation [ID -#{conversation.display_id}] has been assigned to you."
end
''
end
def push_message
{
title: push_message_title,
title: notification.push_message_title,
tag: "#{notification.notification_type}_#{conversation.display_id}",
url: push_url
}
@@ -78,11 +66,14 @@ class Notification::PushNotificationService
return unless subscription.fcm?
fcm = FCM.new(ENV['FCM_SERVER_KEY'])
options = { "notification": {
"title": notification.notification_type.titleize,
"body": push_message_title,
"notification": notification.to_json
} }
options = {
"notification": {
"title": notification.notification_type.titleize,
"body": notification.push_message_title
},
"data": { notification: notification.push_event_data.to_json }
}
response = fcm.send([subscription.subscription_attributes['push_token']], options)
subscription.destroy! if JSON.parse(response[:body])['results']&.first&.keys&.include?('error')
end

View File

@@ -0,0 +1,18 @@
json.data do
json.meta do
json.unread_count @unread_count
end
json.payload do
json.array! @notifications do |notification|
json.id notification.id
json.notification_type notification.notification_type
json.push_message_title notification.push_message_title
json.primary_actor notification.primary_actor&.push_event_data
json.read_at notification.read_at
json.secondary_actor notification.secondary_actor&.push_event_data
json.user notification.user&.push_event_data
json.created_at notification.created_at.to_i
end
end
end