2022-09-11 23:20:10 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-09-11 22:34:18 +02:00
|
|
|
module User::PushNotificationMethods
|
|
|
|
def push_notification(app, resource)
|
|
|
|
raise ArgumentError("Resource must respond to `as_push_notification`") unless resource.respond_to? :as_push_notification
|
|
|
|
|
2022-12-28 00:22:51 +01:00
|
|
|
web_push_subscriptions.active.find_each do |s|
|
2022-09-11 22:34:18 +02:00
|
|
|
n = Rpush::Webpush::Notification.new
|
|
|
|
n.app = app
|
|
|
|
n.registration_ids = [s.subscription.symbolize_keys]
|
|
|
|
n.data = {
|
2023-02-25 15:46:34 +01:00
|
|
|
message: resource.as_push_notification.merge(notification_data).to_json,
|
2022-09-11 22:34:18 +02:00
|
|
|
}
|
|
|
|
n.save!
|
2022-12-25 19:08:55 +01:00
|
|
|
|
|
|
|
PushNotificationWorker.perform_async(n.id)
|
2022-09-11 22:34:18 +02:00
|
|
|
end
|
|
|
|
end
|
2023-02-25 15:46:34 +01:00
|
|
|
|
|
|
|
def notification_data = {
|
|
|
|
data: {
|
2023-03-04 17:49:24 +01:00
|
|
|
badge: unread_inbox_count,
|
2023-02-25 15:46:34 +01:00
|
|
|
},
|
|
|
|
}
|
2022-09-11 22:34:18 +02:00
|
|
|
end
|