2022-09-11 14:20:10 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-09-11 13:34:18 -07: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-27 15:22:51 -08:00
|
|
|
web_push_subscriptions.active.find_each do |s|
|
2022-09-11 13:34:18 -07:00
|
|
|
n = Rpush::Webpush::Notification.new
|
|
|
|
n.app = app
|
|
|
|
n.registration_ids = [s.subscription.symbolize_keys]
|
|
|
|
n.data = {
|
2023-02-25 06:46:34 -08:00
|
|
|
message: resource.as_push_notification.merge(notification_data).to_json,
|
2022-09-11 13:34:18 -07:00
|
|
|
}
|
|
|
|
n.save!
|
2022-12-25 10:08:55 -08:00
|
|
|
|
|
|
|
PushNotificationWorker.perform_async(n.id)
|
2022-09-11 13:34:18 -07:00
|
|
|
end
|
|
|
|
end
|
2023-02-25 06:46:34 -08:00
|
|
|
|
|
|
|
def notification_data = {
|
|
|
|
data: {
|
|
|
|
badge: current_user.unread_inbox_count,
|
|
|
|
},
|
|
|
|
}
|
2022-09-11 13:34:18 -07:00
|
|
|
end
|